Skip to content

Commit

Permalink
Add function to simplify errorFromResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
caleballdrin committed Jan 8, 2025
1 parent 5989655 commit fa7974a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pages/api/graphql-rest.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ class MpdxRestApi extends RESTDataSource {
}
}

// parsedBody has the unknown type so we have to check that it is an object with the expected structure
getRestError = (parsedBody: unknown) =>
parsedBody &&
typeof parsedBody === 'object' &&
'errors' in parsedBody &&
Array.isArray(parsedBody.errors)
? parsedBody.errors[0]
: null;

protected async errorFromResponse({
response,
parsedBody,
Expand All @@ -178,14 +187,8 @@ class MpdxRestApi extends RESTDataSource {
parsedBody: unknown;
}): Promise<GraphQLError> {
const error = await super.errorFromResponse({ response, parsedBody });
// parsedBody has the unknown type so we have to check that it is an object with the expected structure
const restError =
parsedBody &&
typeof parsedBody === 'object' &&
'errors' in parsedBody &&
Array.isArray(parsedBody.errors)
? parsedBody.errors[0]
: null;
const restError = this.getRestError(parsedBody);

if (restError?.detail) {
// Populate the error message with the message detail from the API
error.message = restError.detail;
Expand Down

0 comments on commit fa7974a

Please sign in to comment.