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

CancelledError.md

273 lines4.9 KB

1---
2id: CancelledError
3title: CancelledError
4---
5​
6Defined in: [packages/query-core/src/retryer.ts:120](https://github.com/TanStack/query/blob/main/packages/query-core/src/retryer.ts#L120)
7​
8The error thrown by a `Retryer` (and surfaced to `query.promise`/`mutation`) when a fetch is cancelled, e.g. via
9`query.cancel()`. `revert`, if `true`, tells the caller to restore the state the query was in before the fetch
10started instead of surfacing the error. `silent`, if `true`, tells the caller to suppress this error and instead
11resolve with the promise of the fetch that triggered the cancellation.
12​
13## Example
14​
15```ts
16query.cancel()
17​
18try {
19 await query.promise
20} catch (error) {
21 if (error instanceof CancelledError) {
22 // the fetch was cancelled, e.g. via `query.cancel()`
23 }
25```
26​
27## Extends
28​
29- `Error`
30​
31## Constructors
32​
33### Constructor
34​
35```ts
36new CancelledError(options?: CancelOptions): CancelledError;
37```
38​
39Defined in: [packages/query-core/src/retryer.ts:123](https://github.com/TanStack/query/blob/main/packages/query-core/src/retryer.ts#L123)
40​
41#### Parameters
42​
43##### options?
44​
45[`CancelOptions`](../interfaces/CancelOptions.md)
46​
47#### Returns
48​
49`CancelledError`
50​
51#### Overrides
52​
53```ts
54Error.constructor
55```
56​
57## Properties
58​
59### cause?
60​
61```ts
62optional cause?: unknown;
63```
64​
65Defined in: node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es2022.error.d.ts:24
66​
67#### Inherited from
68​
69```ts
70Error.cause
71```
72​
73***
74​
75### message
76​
77```ts
78message: string;
79```
80​
81Defined in: node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1075
82​
83#### Inherited from
84​
85```ts
86Error.message
87```
88​
89***
90​
91### name
92​
93```ts
94name: string;
95```
96​
97Defined in: node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1074
98​
99#### Inherited from
100​
101```ts
102Error.name
103```
104​
105***
106​
107### revert?
108​
109```ts
110optional revert?: boolean;
111```
112​
113Defined in: [packages/query-core/src/retryer.ts:121](https://github.com/TanStack/query/blob/main/packages/query-core/src/retryer.ts#L121)
114​
115***
116​
117### silent?
118​
119```ts
120optional silent?: boolean;
121```
122​
123Defined in: [packages/query-core/src/retryer.ts:122](https://github.com/TanStack/query/blob/main/packages/query-core/src/retryer.ts#L122)
124​
125***
126​
127### stack?
128​
129```ts
130optional stack?: string;
131```
132​
133Defined in: node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1076
134​
135#### Inherited from
136​
137```ts
138Error.stack
139```
140​
141***
142​
143### stackTraceLimit
144​
145```ts
146static stackTraceLimit: number;
147```
148​
149Defined in: node\_modules/.pnpm/@types+node@22.19.15/node\_modules/@types/node/globals.d.ts:68
150​
151The `Error.stackTraceLimit` property specifies the number of stack frames
152collected by a stack trace (whether generated by `new Error().stack` or
153`Error.captureStackTrace(obj)`).
154​
155The default value is `10` but may be set to any valid JavaScript number. Changes
156will affect any stack trace captured _after_ the value has been changed.
157​
158If set to a non-number value, or set to a negative number, stack traces will
159not capture any frames.
160​
161#### Inherited from
162​
163```ts
164Error.stackTraceLimit
165```
166​
167## Methods
168​
169### captureStackTrace()
170​
171```ts
172static captureStackTrace(targetObject: object, constructorOpt?: Function): void;
173```
174​
175Defined in: node\_modules/.pnpm/@types+node@22.19.15/node\_modules/@types/node/globals.d.ts:52
176​
177Creates a `.stack` property on `targetObject`, which when accessed returns
178a string representing the location in the code at which
179`Error.captureStackTrace()` was called.
180​
181```js
182const myObject = {};
183Error.captureStackTrace(myObject);
184myObject.stack; // Similar to `new Error().stack`
185```
186​
187The first line of the trace will be prefixed with
188`${myObject.name}: ${myObject.message}`.
189​
190The optional `constructorOpt` argument accepts a function. If given, all frames
191above `constructorOpt`, including `constructorOpt`, will be omitted from the
192generated stack trace.
193​
194The `constructorOpt` argument is useful for hiding implementation
195details of error generation from the user. For instance:
196​
197```js
198function a() {
199 b();
201​
202function b() {
203 c();
205​
206function c() {
207 // Create an error without stack trace to avoid calculating the stack trace twice.
208 const { stackTraceLimit } = Error;
209 Error.stackTraceLimit = 0;
210 const error = new Error();
211 Error.stackTraceLimit = stackTraceLimit;
212​
213 // Capture the stack trace above function b
214 Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
215 throw error;
217​
218a();
219```
220​
221#### Parameters
222​
223##### targetObject
224​
225`object`
226​
227##### constructorOpt?
228​
229`Function`
230​
231#### Returns
232​
233`void`
234​
235#### Inherited from
236​
237```ts
238Error.captureStackTrace
239```
240​
241***
242​
243### prepareStackTrace()
244​
245```ts
246static prepareStackTrace(err: Error, stackTraces: CallSite[]): any;
247```
248​
249Defined in: node\_modules/.pnpm/@types+node@22.19.15/node\_modules/@types/node/globals.d.ts:56
250​
251#### Parameters
252​
253##### err
254​
255`Error`
256​
257##### stackTraces
258​
259`CallSite`[]
260​
261#### Returns
262​
263`any`
264​
265#### See
266​
267https://v8.dev/docs/stack-trace-api#customizing-stack-traces
268​
269#### Inherited from
270​
271```ts
272Error.prepareStackTrace
273```