Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

QueryStrictModeError.ts

109 lines2.9 KB

1import type {
2 QueryStrictModeEvent,
3 QueryStrictModeMissingPrefetchEvent,
4 QueryStrictModeUnusedPrefetchEvent,
5} from './QueryStrictModeTypes'
6​
7/**
8 * The base class of the errors for the problems found by `QueryStrictMode`: a
9 * {@link QueryStrictModeMissingPrefetchError} or a {@link QueryStrictModeUnusedPrefetchError}. Use
10 * it to catch both in an error boundary.
11 * @example
12 * ```tsx
13 * <ErrorBoundary
14 * fallbackRender={({ error }) =>
15 * error instanceof QueryStrictModeError ? (
16 * <pre>
17 * {error.event.type}: {error.event.query.queryHash}
18 * </pre>
19 * ) : (
20 * <GenericError />
21 * )
22 * }
23 * >
24 * <QueryStrictMode>
25 * <Routes />
26 * </QueryStrictMode>
27 * </ErrorBoundary>
28 * ```
29 */
30export class QueryStrictModeError extends Error {
31 /**
32 * The problem the error was created for.
33 */
34 event: QueryStrictModeEvent
35 /**
36 * Creates the error for a problem found by `QueryStrictMode`. Passes down `event.message` as the
37 * `Error.message`. Only called by the errors for each problem.
38 * @param event - The problem the error is for.
39 */
40 protected constructor(event: QueryStrictModeEvent) {
41 super(event.message)
42 this.name = 'QueryStrictModeError'
43 this.event = event
44 }
46​
47/**
48 * The error for a {@link QueryStrictModeMissingPrefetchEvent}. The default `handleMissingPrefetch`
49 * throws it.
50 * @example
51 * ```tsx
52 * <ErrorBoundary
53 * fallbackRender={({ error }) =>
54 * error instanceof QueryStrictModeMissingPrefetchError ? (
55 * <pre>Prefetch {error.event.query.queryHash} in the route loader</pre>
56 * ) : (
57 * <GenericError />
58 * )
59 * }
60 * >
61 * <QueryStrictMode>
62 * <Routes />
63 * </QueryStrictMode>
64 * </ErrorBoundary>
65 * ```
66 */
67export class QueryStrictModeMissingPrefetchError extends QueryStrictModeError {
68 /**
69 * The missing prefetch the error was created for.
70 */
71 declare event: QueryStrictModeMissingPrefetchEvent
72 /**
73 * Creates the error for a missing prefetch.
74 * @param event - The missing prefetch the error is for.
75 */
76 constructor(event: QueryStrictModeMissingPrefetchEvent) {
77 super(event)
78 this.name = 'QueryStrictModeMissingPrefetchError'
79 }
81​
82/**
83 * The error for a {@link QueryStrictModeUnusedPrefetchEvent}. The default `handleUnusedPrefetch`
84 * only logs, so throw it from your own.
85 * @example
86 * ```tsx
87 * <QueryStrictMode
88 * handleUnusedPrefetch={(event) => {
89 * throw new QueryStrictModeUnusedPrefetchError(event)
90 * }}
91 * >
92 * <Routes />
93 * </QueryStrictMode>
94 * ```
95 */
96export class QueryStrictModeUnusedPrefetchError extends QueryStrictModeError {
97 /**
98 * The unused prefetch the error was created for.
99 */
100 declare event: QueryStrictModeUnusedPrefetchEvent
101 /**
102 * Creates the error for an unused prefetch.
103 * @param event - The unused prefetch the error is for.
104 */
105 constructor(event: QueryStrictModeUnusedPrefetchEvent) {
106 super(event)
107 this.name = 'QueryStrictModeUnusedPrefetchError'
108 }