mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 16:42:56 +00:00
refactor: improve library account views
This commit is contained in:
36
src/app/_helpers/service-handler.interceptor.ts
Normal file
36
src/app/_helpers/service-handler.interceptor.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {
|
||||
HttpRequest,
|
||||
HttpHandler,
|
||||
HttpEvent,
|
||||
HttpInterceptor,
|
||||
HttpErrorResponse,
|
||||
} from '@angular/common/http';
|
||||
import {Observable, throwError} from 'rxjs';
|
||||
import {NGXLogger} from 'ngx-logger';
|
||||
import {catchError} from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class ServiceHandlerInterceptor implements HttpInterceptor {
|
||||
constructor(private readonly logger: NGXLogger) {}
|
||||
|
||||
intercept(
|
||||
request: HttpRequest<unknown>,
|
||||
next: HttpHandler,
|
||||
): Observable<HttpEvent<unknown>> {
|
||||
return next.handle(request).pipe(
|
||||
// Fixes the issue of errors dropping into "toPromise()"
|
||||
// and being not able to catch it in the "caller methods"
|
||||
catchError((error: HttpErrorResponse) => {
|
||||
const errorMessage =
|
||||
error.error instanceof ErrorEvent
|
||||
? `Error: ${error.error.message}`
|
||||
: `Error Code: ${error.status}, Message: ${error.message}`;
|
||||
|
||||
this.logger.error(errorMessage);
|
||||
|
||||
return throwError(errorMessage);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user