Skip to content

Commit 12f8083

Browse files
committed
docs: added error handler example
1 parent d78ab6d commit 12f8083

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

docs/content/guide/plugins.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,52 @@ useClient({
316316
It is important that you don't use ES6 destructing if you plan to use the `response` property as it will be set after the query is executed, destructing it at the function level will always yield `undefined`.
317317

318318
</doc-tip>
319+
320+
## Example: Global Error Handler
321+
322+
If you are using an error reporting or bug tracking service like [`Sentry`](https://sentry.io/), it can become tedious to handle each query and mutation errors all over your app.
323+
324+
It would be useful to handle most of the errors in a global handler while leaving the specific errors (e.g: validation) to the component that used that query/mutation.
325+
326+
In this example, a global error handler is created where it reports all 500 and unknown errors to sentry.
327+
328+
```ts
329+
import { captureException } from '@sentry/browser';
330+
331+
/**
332+
* Reports unknown errors to sentry to avoid having to do that ever where.
333+
*/
334+
const sentryReportPlugin = definePlugin(({ operation, afterQuery }) => {
335+
afterQuery(({ error }, { response }) => {
336+
// collect some information about the query that failed
337+
const operationContext = {
338+
query: operation.query,
339+
type: operation.type,
340+
variables: operation.variables,
341+
};
342+
343+
if ((response?.status || 200) >= 500) {
344+
captureException(error, {
345+
contexts: {
346+
info: {
347+
description: 'received 500 response code from API',
348+
},
349+
operation: operationContext,
350+
},
351+
});
352+
}
353+
354+
// Other kinds of error codes should be handled by the consuming component
355+
if (error && isUnknownError(error)) {
356+
captureException(error, {
357+
contexts: {
358+
operation: operationContext,
359+
},
360+
});
361+
}
362+
363+
// Notify user about the error
364+
// ...
365+
});
366+
});
367+
```

0 commit comments

Comments
 (0)