@@ -101,15 +101,24 @@ function fetchGraphQL(string $query): stdClass
101
101
{
102
102
$ ch = getGraphQLCurlHandle ($ query );
103
103
$ response = curl_exec ($ ch );
104
+ curl_close ($ ch );
105
+ $ obj = json_decode ($ response );
104
106
// 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
106
111
if (str_contains (curl_error ($ ch ), 'unable to get local issuer certificate ' )) {
107
112
throw new AssertionError ("You don't have a valid SSL Certificate installed or XAMPP. " );
108
113
}
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 --> " );
110
120
}
111
- curl_close ($ ch );
112
- return json_decode ($ response );
121
+ return $ obj ;
113
122
}
114
123
115
124
/**
@@ -140,6 +149,11 @@ function getContributionYears(string $user): array
140
149
// Other errors that contain a message field
141
150
throw new InvalidArgumentException ($ response ->data ->errors [0 ]->message );
142
151
}
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
+ }
143
157
return $ response ->data ->user ->contributionsCollection ->contributionYears ;
144
158
}
145
159
0 commit comments