Skip to content

Commit

Permalink
update: handle catch error inside streamCreatorCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeyyeKurtulus committed Oct 3, 2024
1 parent 136edf5 commit c8caa03
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
25 changes: 14 additions & 11 deletions npm/ng-packs/packages/core/src/lib/services/list.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable, Injector, OnDestroy } from '@angular/core';
import {
EMPTY,
BehaviorSubject,
MonoTypeOperatorFunction,
Observable,
of,
ReplaySubject,
Subject,
} from 'rxjs';
Expand Down Expand Up @@ -130,16 +130,19 @@ export class ListService<QueryParamsType = ABP.PageQueryParams | any> implements
return this.query$.pipe(
tap(() => this._isLoading$.next(true)),
tap(() => this._requestStatus.next('loading')),
switchMap(query => streamCreatorCallback(query).pipe(catchError(() => of(null)))),
filter(Boolean),
tap(() => this._isLoading$.next(false)),
tap(() => this._requestStatus.next('success')),
shareReplay<any>({ bufferSize: 1, refCount: true }),
takeUntil(this.destroy$),
catchError(error => {
this._requestStatus.next('error');
throw error;
}),
switchMap(query =>
streamCreatorCallback(query).pipe(
catchError(() => {
this._requestStatus.next('error');
return EMPTY;
}),
filter(Boolean),
tap(() => this._isLoading$.next(false)),
tap(() => this._requestStatus.next('success')),
shareReplay<any>({ bufferSize: 1, refCount: true }),
takeUntil(this.destroy$),
),
),
);
}

Expand Down
6 changes: 4 additions & 2 deletions npm/ng-packs/packages/core/src/lib/tests/list.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('ListService', () => {
service.hookToQuery(callback).subscribe();
});

it('should emit error requestStatus as side effect', done => {
it('should emit error requestStatus as side effect and stop processing', done => {
const errCallback: QueryStreamCreatorCallback<ABP.PageQueryParams> = query => {
throw Error('A server error occurred');
};
Expand All @@ -177,7 +177,9 @@ describe('ListService', () => {
done();
});

service.hookToQuery(errCallback).subscribe();
service.hookToQuery(errCallback).subscribe({
error: () => done(),
});
});
});
});

0 comments on commit c8caa03

Please sign in to comment.