mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-19 08:02:55 +00:00
37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
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(error);
|
|
}),
|
|
);
|
|
}
|
|
}
|