-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Remove exports of named errors #6705
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
11ba7e0
Remove exports of named errors
IvanGoncharov cabdb52
Continue to pass the original Error to executionDidEnd
glasser b64028e
GraphQLErrorWithCode can set this.name
glasser 710fb3a
Move new enum to its own export (eg, for use in clients)
glasser 1da462b
Add INTERNAL_SERVER_ERROR to the enum
glasser 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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@apollo/server/errors", | ||
"type": "module", | ||
"main": "../dist/cjs/errors/index.js", | ||
"module": "../dist/esm/errors/index.js", | ||
"types": "../dist/esm/errors/index.d.ts", | ||
"sideEffects": false | ||
} |
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
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
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// The functions in this file are not part of Apollo Server's external API. | ||
|
||
import { | ||
GraphQLError, | ||
GraphQLErrorExtensions, | ||
GraphQLFormattedError, | ||
} from 'graphql'; | ||
|
||
declare module 'graphql' { | ||
export interface GraphQLErrorExtensions { | ||
code?: string; | ||
exception?: { | ||
stacktrace?: ReadonlyArray<string>; | ||
}; | ||
} | ||
} | ||
|
||
// This function accepts any value that were thrown and convert it to GraphQLFormatterError. | ||
// It also add default extensions.code and copy stack trace onto an extension if requested. | ||
// This function should not throw. | ||
export function normalizeAndFormatErrors( | ||
errors: ReadonlyArray<unknown>, | ||
options: { | ||
formatError?: ( | ||
formattedError: GraphQLFormattedError, | ||
error: unknown, | ||
) => GraphQLFormattedError; | ||
includeStackTracesInErrorResponses?: boolean; | ||
} = {}, | ||
): Array<GraphQLFormattedError> { | ||
const formatError = options.formatError ?? ((error) => error); | ||
return errors.map((error) => { | ||
try { | ||
return formatError(enrichError(error), error); | ||
} catch (formattingError) { | ||
if (options.includeStackTracesInErrorResponses) { | ||
// includeStackTracesInErrorResponses is used in development | ||
// so it will be helpful to show errors thrown by formatError hooks in that mode | ||
return enrichError(formattingError); | ||
} else { | ||
// obscure error | ||
return { | ||
message: 'Internal server error', | ||
extensions: { code: 'INTERNAL_SERVER_ERROR' }, | ||
}; | ||
} | ||
} | ||
}); | ||
|
||
function enrichError(maybeError: unknown): GraphQLFormattedError { | ||
const graphqlError = ensureGraphQLError(maybeError); | ||
|
||
const extensions: GraphQLErrorExtensions = { | ||
...graphqlError.extensions, | ||
code: graphqlError.extensions.code ?? 'INTERNAL_SERVER_ERROR', | ||
}; | ||
|
||
const { originalError } = graphqlError; | ||
if (originalError != null && !(originalError instanceof GraphQLError)) { | ||
const originalErrorEnumerableEntries = Object.entries( | ||
originalError, | ||
).filter(([key]) => key !== 'extensions'); | ||
|
||
if (originalErrorEnumerableEntries.length > 0) { | ||
extensions.exception = { | ||
...extensions.exception, | ||
...Object.fromEntries(originalErrorEnumerableEntries), | ||
}; | ||
} | ||
} | ||
|
||
if (options.includeStackTracesInErrorResponses) { | ||
extensions.exception = { | ||
...extensions.exception, | ||
stacktrace: graphqlError.stack?.split('\n'), | ||
}; | ||
} | ||
|
||
return { ...graphqlError.toJSON(), extensions }; | ||
} | ||
} | ||
|
||
export function ensureError(maybeError: unknown): Error { | ||
return maybeError instanceof Error | ||
? maybeError | ||
: new GraphQLError('Unexpected error value: ' + String(maybeError)); | ||
} | ||
|
||
export function ensureGraphQLError(maybeError: unknown): GraphQLError { | ||
const error: Error = ensureError(maybeError); | ||
|
||
return error instanceof GraphQLError | ||
? error | ||
: new GraphQLError(error.message, { originalError: error }); | ||
} |
Oops, something went wrong.
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.
Seems like this
stillnow belongs insrc/errors/
but fine either wayThere 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.
Hmm. My thought was that since this code is entirely consumed outside of that sub-export, then it didn't make sense for it to be in the subdirectory? I guess that the tree shaking bundlers we care about probably won't scoop up those files if you don't do the top export, but since the whole point is to minimize
src/errors
to the stuff that clients might need it seemed like a bad idea to put unrelated stuff there. I thought about doing src/internalErrors or something but it seemed like overkill. Thoughts?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.
Fine as is, didn't feel too strongly about this. Agree with not doing an
internalErrors
folder.