@@ -101,15 +101,24 @@ function fetchGraphQL(string $query): stdClass
101101{
102102 $ ch = getGraphQLCurlHandle ($ query );
103103 $ response = curl_exec ($ ch );
104+ curl_close ($ ch );
105+ $ obj = json_decode ($ response );
104106 // handle curl errors
105- if ($ response === false ) {
107+ if ($ response === false || $ obj === null || curl_getinfo ($ ch , CURLINFO_HTTP_CODE ) >= 400 ) {
108+ // set response code to curl error code
109+ http_response_code (curl_getinfo ($ ch , CURLINFO_HTTP_CODE ));
110+ // Missing SSL certificate
106111 if (str_contains (curl_error ($ ch ), 'unable to get local issuer certificate ' )) {
107112 throw new AssertionError ("You don't have a valid SSL Certificate installed or XAMPP. " );
108113 }
109- throw new AssertionError ("An error occurred when getting a response from GitHub. " );
114+ // Handle errors such as "Bad credentials"
115+ if ($ obj && $ obj ->message ) {
116+ throw new AssertionError ("Error: $ obj ->message \n<!-- $ response --> " );
117+ }
118+ // TODO: Make the $response part get passed into a custom error and render the commented details in the SVG card generator
119+ throw new AssertionError ("An error occurred when getting a response from GitHub. \n<!-- $ response --> " );
110120 }
111- curl_close ($ ch );
112- return json_decode ($ response );
121+ return $ obj ;
113122}
114123
115124/**
@@ -140,6 +149,11 @@ function getContributionYears(string $user): array
140149 // Other errors that contain a message field
141150 throw new InvalidArgumentException ($ response ->data ->errors [0 ]->message );
142151 }
152+ // API did not return data
153+ if (!isset ($ response ->data ) && isset ($ response ->message )) {
154+ // Other errors that contain a message field
155+ throw new InvalidArgumentException ($ response ->message );
156+ }
143157 return $ response ->data ->user ->contributionsCollection ->contributionYears ;
144158}
145159
0 commit comments