From e35b8aba785f7356269a0f15dc776fef2b23bb45 Mon Sep 17 00:00:00 2001 From: Gordon Hirsch Date: Thu, 7 Nov 2024 14:44:31 -0500 Subject: [PATCH] report as expected errors graphQL failures with internal errors --- .../cli-kit/src/private/node/api/graphql.ts | 8 +---- packages/cli-kit/src/public/node/error.ts | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/cli-kit/src/private/node/api/graphql.ts b/packages/cli-kit/src/private/node/api/graphql.ts index 7975e00baa..8979b87748 100644 --- a/packages/cli-kit/src/private/node/api/graphql.ts +++ b/packages/cli-kit/src/private/node/api/graphql.ts @@ -1,7 +1,6 @@ import {GraphQLClientError, sanitizedHeadersOutput} from './headers.js' import {sanitizeURL} from './urls.js' import {stringifyMessage, outputContent, outputToken, outputDebug} from '../../../public/node/output.js' -import {AbortError} from '../../../public/node/error.js' import {ClientError, Variables} from 'graphql-request' export function debugLogRequestInfo( @@ -43,12 +42,7 @@ ${outputToken.json(error.response.errors)} Request ID: ${requestId} ` } - let mappedError: Error - if (status < 500) { - mappedError = new GraphQLClientError(errorMessage, status, error.response.errors) - } else { - mappedError = new AbortError(errorMessage) - } + const mappedError: Error = new GraphQLClientError(errorMessage, status, error.response.errors) mappedError.stack = error.stack return mappedError } else { diff --git a/packages/cli-kit/src/public/node/error.ts b/packages/cli-kit/src/public/node/error.ts index af348c4d85..e204415c67 100644 --- a/packages/cli-kit/src/public/node/error.ts +++ b/packages/cli-kit/src/public/node/error.ts @@ -133,7 +133,7 @@ export async function handler(error: unknown): Promise { } else if (typeof error === 'string') { fatal = new BugError(error) } else if (error instanceof Error) { - fatal = new BugError(error.message) + fatal = isGraphQLError(error) ? new AbortError(error.message) : new BugError(error.message) fatal.stack = error.stack } else { // errors can come in all shapes and sizes... @@ -188,6 +188,9 @@ function isFatal(error: unknown): error is FatalError { */ export function shouldReportErrorAsUnexpected(error: unknown): boolean { if (!isFatal(error)) { + if (isInternalGraphQLError(error)) { + return false + } // this means its not one of the CLI wrapped errors if (error instanceof Error) { const message = error.message @@ -234,3 +237,31 @@ function errorMessageImpliesEnvironmentIssue(message: string): boolean { const anyMatches = environmentIssueMessages.some((issueMessage) => message.includes(issueMessage)) return anyMatches } + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function isInternalGraphQLError(error: any): boolean { + if (!isGraphQLError(error)) { + return false + } + + const extensions = error?.errors?.[0]?.extensions + + // App Management errors have a category that can be used to determine if the error is internal + const category: string | undefined = extensions?.app_errors?.errors?.[0]?.category + if (category) { + return category === 'internal' + } + + // Partners errors have a code that can be used to determine if the error is internal + const code: string | undefined = extensions?.code + if (code) { + return parseInt(code, 10) >= 500 + } + + return true +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function isGraphQLError(error: any): boolean { + return error?.name === 'GraphQLClientError' +}