Skip to content

Commit 1358878

Browse files
committed
BREAKING_CHANGE: modify throwOnError option work for queries and methods refetch and start (query), now with this flag(true) and using start\refetch user should catch error
1 parent 3bd245b commit 1358878

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

src/mobx-inifinite-query.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,20 @@ export class MobxInfiniteQuery<
309309
}
310310

311311
async refetch(options?: RefetchOptions) {
312-
return await this.queryObserver.refetch(options);
312+
const result = await this.queryObserver.refetch(options);
313+
const query = this.queryObserver.getCurrentQuery();
314+
315+
if (
316+
query.state.error &&
317+
(options?.throwOnError ||
318+
this.options.throwOnError === true ||
319+
(typeof this.options.throwOnError === 'function' &&
320+
this.options.throwOnError(query.state.error, query)))
321+
) {
322+
throw query.state.error;
323+
}
324+
325+
return result;
313326
}
314327

315328
async reset(params?: MobxInfiniteQueryResetParams) {

src/mobx-query.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,4 +1282,76 @@ describe('MobxQuery', () => {
12821282
expect(getDynamicOptionsSpy).toBeCalledTimes(3);
12831283
});
12841284
});
1285+
1286+
describe('throwOnError', () => {
1287+
it('should throw error (throwOnError: true in options)', async () => {
1288+
vi.useFakeTimers();
1289+
const query = new MobxQueryMock({
1290+
throwOnError: true,
1291+
enabled: false,
1292+
queryFn: async () => {
1293+
throw new Error('MobxQueryError');
1294+
},
1295+
});
1296+
let error: Error | undefined;
1297+
1298+
const promise = query.start().catch((error_) => {
1299+
error = error_;
1300+
});
1301+
await vi.runAllTimersAsync();
1302+
1303+
await promise;
1304+
1305+
expect(error?.message).toBe('MobxQueryError');
1306+
});
1307+
1308+
it('should throw error (updating param throwOnError true)', async () => {
1309+
vi.useFakeTimers();
1310+
const query = new MobxQueryMock({
1311+
enabled: false,
1312+
queryFn: async () => {
1313+
throw new Error('MobxQueryError');
1314+
},
1315+
});
1316+
let error: Error | undefined;
1317+
1318+
const promise = query.start({ throwOnError: true }).catch((error_) => {
1319+
error = error_;
1320+
});
1321+
await vi.runAllTimersAsync();
1322+
1323+
await promise;
1324+
1325+
expect(error?.message).toBe('MobxQueryError');
1326+
});
1327+
1328+
it('should throw error (throwOnError: true in global options)', async () => {
1329+
vi.useFakeTimers();
1330+
const query = new MobxQueryMock(
1331+
{
1332+
enabled: false,
1333+
queryFn: async () => {
1334+
throw new Error('MobxQueryError');
1335+
},
1336+
},
1337+
new QueryClient({
1338+
defaultOptions: {
1339+
queries: {
1340+
throwOnError: true,
1341+
},
1342+
},
1343+
}),
1344+
);
1345+
let error: Error | undefined;
1346+
1347+
const promise = query.start().catch((error_) => {
1348+
error = error_;
1349+
});
1350+
await vi.runAllTimersAsync();
1351+
1352+
await promise;
1353+
1354+
expect(error?.message).toBe('MobxQueryError');
1355+
});
1356+
});
12851357
});

src/mobx-query.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,20 @@ export class MobxQuery<
171171
}
172172

173173
async refetch(options?: RefetchOptions) {
174-
return await this.queryObserver.refetch(options);
174+
const result = await this.queryObserver.refetch(options);
175+
const query = this.queryObserver.getCurrentQuery();
176+
177+
if (
178+
query.state.error &&
179+
(options?.throwOnError ||
180+
this.options.throwOnError === true ||
181+
(typeof this.options.throwOnError === 'function' &&
182+
this.options.throwOnError(query.state.error, query)))
183+
) {
184+
throw query.state.error;
185+
}
186+
187+
return result;
175188
}
176189

177190
protected createQueryHash(
@@ -350,7 +363,8 @@ export class MobxQuery<
350363
...params,
351364
};
352365
this.update(options);
353-
return await this.refetch();
366+
367+
await this.refetch();
354368
}
355369

356370
/**

0 commit comments

Comments
 (0)