-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
expose setErrorMessageHandler
#11694
Changes from 3 commits
c8762ff
15c6145
3d8b7aa
1a90eee
12b06eb
843adbc
5018738
5bc6188
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
expose `setErrorMessageHandler` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { loadDevMessages } from "./loadDevMessages.js"; | ||
export { loadErrorMessageHandler } from "./loadErrorMessageHandler.js"; | ||
export { loadErrorMessages } from "./loadErrorMessages.js"; | ||
export { setErrorMessageHandler } from "./setErrorMessageHandler.js"; | ||
export type { ErrorMessageHandler } from "./setErrorMessageHandler.js"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
import type { ErrorCodes } from "../invariantErrorCodes.js"; | ||
import { global } from "../utilities/globals/index.js"; | ||
import { ApolloErrorMessageHandler } from "../utilities/globals/invariantWrappers.js"; | ||
import type { ErrorMessageHandler } from "./setErrorMessageHandler.js"; | ||
import { setErrorMessageHandler } from "./setErrorMessageHandler.js"; | ||
|
||
/** | ||
* Injects Apollo Client's default error message handler into the application and | ||
* also loads the error codes that are passed in as arguments. | ||
*/ | ||
export function loadErrorMessageHandler(...errorCodes: ErrorCodes[]) { | ||
if (!global[ApolloErrorMessageHandler]) { | ||
global[ApolloErrorMessageHandler] = handler as typeof handler & ErrorCodes; | ||
} | ||
setErrorMessageHandler(handler as typeof handler & ErrorCodes); | ||
|
||
for (const codes of errorCodes) { | ||
Object.assign(global[ApolloErrorMessageHandler], codes); | ||
Object.assign(handler, codes); | ||
} | ||
|
||
return global[ApolloErrorMessageHandler]; | ||
return handler; | ||
} | ||
|
||
function handler(message: string | number, args: unknown[]) { | ||
if (typeof message === "number") { | ||
const definition = global[ApolloErrorMessageHandler]![message]; | ||
if (!message || !definition?.message) return; | ||
message = definition.message; | ||
} | ||
return args.reduce<string>( | ||
(msg, arg) => msg.replace(/%[sdfo]/, String(arg)), | ||
String(message) | ||
); | ||
const handler = ((message: string | number, args: unknown[]) => { | ||
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. Another thing to note is that this is now on module level, so anything that has loaded into it with |
||
if (typeof message === "number") { | ||
const definition = global[ApolloErrorMessageHandler]![message]; | ||
if (!message || !definition?.message) return; | ||
message = definition.message; | ||
} | ||
} | ||
return args.reduce<string>( | ||
(msg, arg) => msg.replace(/%[sdfo]/, String(arg)), | ||
String(message) | ||
); | ||
}) as ErrorMessageHandler & ErrorCodes; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { ErrorCodes } from "../invariantErrorCodes.js"; | ||
import { global } from "../utilities/globals/index.js"; | ||
import { ApolloErrorMessageHandler } from "../utilities/globals/invariantWrappers.js"; | ||
|
||
/** | ||
* The error message handler is a function that is called when a message is | ||
* logged or an error is thrown to determine the contents of the error message | ||
* to be logged or thrown. | ||
*/ | ||
export type ErrorMessageHandler = { | ||
/** | ||
* @param message - Usually the error message number (as defined in | ||
* `@apollo/client/invariantErrorCodes.js`). | ||
* In some edge cases, this can already be a string, that can be passed through | ||
* as an error message. | ||
* | ||
* @param args - The placeholders that can be passed into the error message. | ||
* These relate with the `%s` and `%d` [substitution strings](https://developer.mozilla.org/en-US/docs/Web/API/console#using_string_substitutions) | ||
* in the error message defined in `@apollo/client/invariantErrorCodes.js`. | ||
* | ||
* @returns The error message to be logged or thrown. If it returns `undefined`, | ||
* the mechanism will fall back to the default: | ||
* A link to https://go.apollo.dev/c/err with Apollo Client version, | ||
* the error message number, and the error message arguments encoded into | ||
* the URL hash. | ||
*/ (message: string | number, args: unknown[]): string | undefined; | ||
}; | ||
|
||
/** | ||
* Overrides the global "Error Message Handler" with a custom implementation. | ||
*/ | ||
export function setErrorMessageHandler(handler: ErrorMessageHandler) { | ||
global[ApolloErrorMessageHandler] = handler as typeof handler & ErrorCodes; | ||
} |
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.
This is a notable change (but probably not very relevant):
If a user had created their own handler, and then called
loadErrorMessageHandler
, previously that would not have overwritten that handler, and now it would.We have never exposed a way of setting that handler before, though, so it probably doesn't matter.