Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Request ID in error message #2717

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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