2

I use interceptor like this

export class ServerErrorInterceptor implements HttpInterceptor {
    constructor(
        private appMessageService: AppMessageService) { }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        return next.handle(request).pipe(
            catchError((error: HttpErrorResponse) => {
                this.appMessageService.handleError(error, 'Server request error');
                
                return of();
            })
        );
    }
}

And use HttpClient with firstValueFrom

    async create(request: any): Promise<void> {
        const response = this.http.post(this.url, request);
        await firstValueFrom(response);
    }

I expect that of() returns an empty result, so awaiting "create" method should not throw an error. But after "catchError" show an error message, another error throws "sequence contains no elements". How to build a handler pipe to not throw a second error?

1

1 Answer 1

1

firstValueFrom requires the source observable to emit at least one value or it will throw the error you are encountering.

If the observable stream completes before any values were emitted, the returned promise will reject with EmptyError or will resolve with the default value if a default was specified.

Instead of of(), you can simply emit undefined:

of(undefined)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.