Skip to content

Commit

Permalink
Merge pull request #2717 from Shopify/include-request-id-in-error-mes…
Browse files Browse the repository at this point in the history
…sage

Include Request ID in error message
  • Loading branch information
amcaplan authored Aug 29, 2023
2 parents d010759 + 6ab0ce1 commit d9cbe5c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-moles-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-kit': patch
---

Include Request ID in error message
4 changes: 2 additions & 2 deletions packages/cli-kit/src/private/node/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const interestingResponseHeaders = new Set(['cache-control', 'content-type', 'et

export async function debugLogResponseInfo<T extends {headers: Headers; status: number}>(
{request, url}: RequestOptions<T>,
errorHandler?: (error: unknown) => Error | unknown,
errorHandler?: (error: unknown, requestId: string | undefined) => Error | unknown,
): Promise<T> {
const t0 = performance.now()
const responseHeaders: {[key: string]: string} = {}
Expand All @@ -38,7 +38,7 @@ export async function debugLogResponseInfo<T extends {headers: Headers; status:
}
}
if (errorHandler) {
throw errorHandler(err)
throw errorHandler(err, responseHeaders['x-request-id'])
} else {
throw err
}
Expand Down
11 changes: 8 additions & 3 deletions packages/cli-kit/src/private/node/api/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ function sanitizeVariables(variables: Variables): string {
return JSON.stringify(result, null, 2)
}

export function errorHandler<T>(api: string): (error: unknown) => Error | unknown {
return (error: unknown) => {
export function errorHandler<T>(api: string): (error: unknown, requestId?: string) => Error | unknown {
return (error: unknown, requestId?: string) => {
if (error instanceof ClientError) {
const errorMessage = stringifyMessage(outputContent`
let errorMessage = stringifyMessage(outputContent`
The ${outputToken.raw(
api,
)} GraphQL API responded unsuccessfully with the HTTP status ${`${error.response.status}`} and errors:
${outputToken.json(error.response.errors)}
`)
if (requestId) {
errorMessage += `
Request ID: ${requestId}
`
}
let mappedError: Error
if (error.response.status < 500) {
mappedError = new GraphQLClientError(errorMessage, error.response.status, error.response.errors)
Expand Down

0 comments on commit d9cbe5c

Please sign in to comment.