Skip to content

Commit 96abc3c

Browse files
committed
fix: use exception for ignoring requests
1 parent a883981 commit 96abc3c

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

src/RequestIgnoredError.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default class RequestIgnoredError extends Error {}

src/createStores.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineStore, type StoreDefinition } from 'pinia';
22
import { ID, ApiNames } from './types';
33
import createValidator from './validateResponse';
4+
import RequestIgnoredError from './RequestIgnoredError';
45

56
type SortSettings = {
67
field: string;
@@ -181,7 +182,9 @@ function createStore(
181182

182183
return api.get(url, params).then((res: any) => {
183184
if (requestKey) {
184-
if (this.requestKeys[requestKey] !== startedAt) return;
185+
if (this.requestKeys[requestKey] !== startedAt) {
186+
throw new RequestIgnoredError();
187+
}
185188
delete this.requestKeys[requestKey];
186189
}
187190
if (settings.envelope === false) {

src/useFetchState.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { computed, ref, useSlots } from 'vue';
22
import ServerError from './ServerError';
3+
import RequestIgnoredError from './RequestIgnoredError';
34

45
export default function useFetchState(loadOnUpdate: boolean) {
56
const hasLoaded = ref(false);
@@ -14,6 +15,10 @@ export default function useFetchState(loadOnUpdate: boolean) {
1415
const isFailed = computed(() => state.value === 'failed');
1516

1617
function handleError(e: any) {
18+
if (e instanceof RequestIgnoredError) {
19+
// The request was ignored, no error needed
20+
return;
21+
}
1722
if (!slots.failed) {
1823
if (e.log) {
1924
e.log('Fetch unhandled:');

0 commit comments

Comments
 (0)