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

expose setErrorMessageHandler #11694

Merged
merged 8 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/eleven-doors-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

expose `setErrorMessageHandler`
2 changes: 2 additions & 0 deletions src/dev/index.ts
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";
36 changes: 20 additions & 16 deletions src/dev/loadErrorMessageHandler.ts
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);
Copy link
Member Author

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.


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[]) => {
Copy link
Member Author

Choose a reason for hiding this comment

The 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 loadErrorMessageHandler will now stay inside of it, and it cannot just be reset by delete global[ApolloErrorMessageHandler] - this might bite us in our tests, (let's see), but probably nobody else.

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;
34 changes: 34 additions & 0 deletions src/dev/setErrorMessageHandler.ts
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;
}
Loading