Skip to content

Commit 3935773

Browse files
committed
fix(sdk): handle graphql errors properly
1 parent 925d40b commit 3935773

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

packages/sdk-util/src/index.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,42 @@ class BaseClient {
284284
),
285285
});
286286

287-
if (res.status === 200) {
288-
const json = await res.json();
289-
debug('_doRequest.response', { status: res.status, errors: json.errors });
290-
291-
if (Array.isArray(json.errors) && json.errors.length) {
292-
const err = new Error(`GraphQLError: ${json.errors.map(x => x.message).join('; ')}`);
293-
err.errors = json.errors;
294-
throw err;
287+
let json;
288+
try {
289+
json = await res.json();
290+
debug('_doRequest.response', { status: res.status, errors: json ? json.errors : undefined });
291+
} catch (err) {
292+
json = {};
293+
}
294+
295+
// Handle GraphQL errors regardless of HTTP status
296+
if (Array.isArray(json.errors) && json.errors.length) {
297+
const errorMessages = json.errors.map(x => x.message);
298+
const hasSchemaError = errorMessages.some(msg => msg.includes('Cannot query field'));
299+
300+
let message = errorMessages.join('\n');
301+
if (hasSchemaError) {
302+
message = `Schema Compatibility Error: ${message}\nThis error typically indicates you are using an incompatible GraphQL client version. Please ensure your client version matches the server schema version.`;
303+
} else {
304+
message = `GraphQLError: ${message}`;
295305
}
296306

297-
return dataKey && json.data[dataKey] ? json.data[dataKey] : json.data;
307+
const err = new Error(message);
308+
err.errors = json.errors;
309+
err.status = res.status;
310+
throw err;
311+
}
312+
313+
// Handle HTTP errors
314+
if (res.status !== 200) {
315+
const message = json.error || json.message || `GraphQL Status Error ${res.status}`;
316+
const err = new Error(message);
317+
err.status = res.status;
318+
err.response = json;
319+
throw err;
298320
}
299321

300-
throw new Error(`GraphQL Status Error ${res.status}`);
322+
return dataKey && json.data[dataKey] ? json.data[dataKey] : json.data;
301323
}
302324

303325
/**
@@ -491,4 +513,3 @@ class BaseClient {
491513
}
492514

493515
module.exports = BaseClient;
494-
//# sourceMappingURL=index.js.map

0 commit comments

Comments
 (0)