-
Notifications
You must be signed in to change notification settings - Fork 129
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
feat: original error as extensions #1514
Merged
saihaj
merged 1 commit into
saihaj/1482-remove-handlevalidationerrors-and-handleparseerrors-options-from-usemaskederrors
from
feat-original-error-as-extensions
Sep 7, 2022
+93
−15
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,39 +6,64 @@ export const DEFAULT_ERROR_MESSAGE = 'Unexpected error.'; | |
export type MaskErrorFn = (error: unknown, message: string) => Error; | ||
|
||
export type SerializableGraphQLErrorLike = Error & { | ||
name: 'GraphQLError'; | ||
toJSON(): { message: string }; | ||
toJSON?(): { message: string }; | ||
extensions?: Record<string, unknown>; | ||
}; | ||
|
||
export function isGraphQLError(error: unknown): error is Error & { originalError?: Error } { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return error instanceof Error && error.name === 'GraphQLError'; | ||
} | ||
|
||
export function createSerializableGraphQLError(message: string): SerializableGraphQLErrorLike { | ||
const error = new Error(message); | ||
function createSerializableGraphQLError( | ||
message: string, | ||
originalError: unknown, | ||
isDev: boolean | ||
): SerializableGraphQLErrorLike { | ||
const error: SerializableGraphQLErrorLike = new Error(message); | ||
error.name = 'GraphQLError'; | ||
if (isDev) { | ||
const extensions = | ||
originalError instanceof Error | ||
? { message: originalError.message, stack: originalError.stack } | ||
: { message: String(originalError) }; | ||
|
||
Object.defineProperty(error, 'extensions', { | ||
get() { | ||
return extensions; | ||
}, | ||
}); | ||
} | ||
|
||
Object.defineProperty(error, 'toJSON', { | ||
value() { | ||
return { | ||
message: error.message, | ||
extensions: error.extensions, | ||
}; | ||
}, | ||
}); | ||
|
||
return error as SerializableGraphQLErrorLike; | ||
} | ||
|
||
export const defaultMaskErrorFn: MaskErrorFn = (err, message) => { | ||
if (isGraphQLError(err)) { | ||
if (err?.originalError) { | ||
if (isGraphQLError(err.originalError)) { | ||
return err; | ||
export const createDefaultMaskErrorFn = | ||
(isDev: boolean): MaskErrorFn => | ||
(error, message) => { | ||
if (isGraphQLError(error)) { | ||
if (error?.originalError) { | ||
if (isGraphQLError(error.originalError)) { | ||
return error; | ||
} | ||
return createSerializableGraphQLError(message, error, isDev); | ||
} | ||
return createSerializableGraphQLError(message); | ||
return error; | ||
} | ||
return err; | ||
} | ||
return createSerializableGraphQLError(message); | ||
}; | ||
return createSerializableGraphQLError(message, error, isDev); | ||
}; | ||
|
||
const isDev = globalThis.process?.env?.NODE_ENV === 'development'; | ||
|
||
export const defaultMaskErrorFn: MaskErrorFn = createDefaultMaskErrorFn(isDev); | ||
|
||
export type UseMaskedErrorsOpts = { | ||
/** The function used for identify and mask errors. */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait I just realized this. Why we remove
name
here? GraphQLError should haveGraphQLError
name as in the followingisGraphQLError
check