Skip to content

Commit

Permalink
fix(query): 🐞 support throwOnError
Browse files Browse the repository at this point in the history
throwOnError true calls observer.error now

βœ… Closes: #180
  • Loading branch information
NetanelBasal committed May 26, 2024
1 parent 39bc985 commit c232b47
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
22 changes: 17 additions & 5 deletions query/src/lib/base-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@tanstack/query-core';
import { Observable, shareReplay } from 'rxjs';
import { normalizeOptions } from './query-options';
import { shouldThrowError } from './utils';

export type QueryFunctionWithObservable<
T = unknown,
Expand Down Expand Up @@ -121,11 +122,22 @@ export function createBaseQuery<

const queryObserverDispose = queryObserver.subscribe(
notifyManager.batchCalls((result) => {
observer.next(
defaultedOptions.notifyOnChangeProps
? result
: queryObserver?.trackResult(result),
);
if (
!result.isFetching &&
result.isError &&
shouldThrowError(queryObserver!.options.throwOnError, [
result.error,
queryObserver!.getCurrentQuery(),
])
) {
observer.error(result.error);
} else {
observer.next(
defaultedOptions.notifyOnChangeProps
? result
: queryObserver?.trackResult(result),
);
}
}),
);

Expand Down
15 changes: 13 additions & 2 deletions query/src/lib/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@tanstack/query-core';
import { isObservable, Observable, shareReplay } from 'rxjs';
import { toSignal } from '@angular/core/rxjs-interop';
import { toPromise } from './utils';
import { shouldThrowError, toPromise } from './utils';

export type CreateMutationOptions<
TData = unknown,
Expand Down Expand Up @@ -89,7 +89,18 @@ class Mutation {
notifyManager.batchCalls(
(
result: MutationObserverResult<TData, TError, TVariables, TContext>,
) => observer.next(result),
) => {
if (
result.isError &&
shouldThrowError(mutationObserver!.options.throwOnError, [
result.error,
])
) {
observer.error(result.error);
} else {
observer.next(result);
}
},
),
);

Expand Down
18 changes: 16 additions & 2 deletions query/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
DefaultError,
QueryObserverResult,
QueryObserverSuccessResult
QueryObserverSuccessResult,
} from '@tanstack/query-core';
import { Observable, Subject, firstValueFrom, takeUntil } from 'rxjs';

Expand Down Expand Up @@ -38,7 +38,10 @@ export function createSuccessObserverResult<T, Error = DefaultError>(
} as QueryObserverSuccessResult<T, Error>;
}

export function createPendingObserverResult<T = unknown, Error = DefaultError>(): QueryObserverResult<T, Error> {
export function createPendingObserverResult<
T = unknown,
Error = DefaultError,
>(): QueryObserverResult<T, Error> {
return {
isError: false,
isLoading: true,
Expand All @@ -49,3 +52,14 @@ export function createPendingObserverResult<T = unknown, Error = DefaultError>()
status: 'pending',
} as QueryObserverResult<T, Error>;
}

export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
throwError: boolean | T | undefined,
params: Parameters<T>,
): boolean {
if (typeof throwError === 'function') {
return throwError(...params);
}

return !!throwError;
}
9 changes: 8 additions & 1 deletion src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { provideHttpClient } from '@angular/common/http';
import { ApplicationConfig, inject } from '@angular/core';
import { ApplicationConfig, ErrorHandler, inject } from '@angular/core';
import { provideRouter, withComponentInputBinding } from '@angular/router';

import {
Expand All @@ -22,8 +22,15 @@ const withFunctionaFactory: QueryClientConfigFn = () => {
};
};

class MyErrorHandler extends ErrorHandler {
override handleError(error: any) {
return super.handleError(error);
}
}

export const appConfig: ApplicationConfig = {
providers: [
{ provide: ErrorHandler, useClass: MyErrorHandler },
provideRouter(appRoutes, withComponentInputBinding()),
provideHttpClient(),
provideQueryClientOptions(withFunctionaFactory),
Expand Down

0 comments on commit c232b47

Please sign in to comment.