Skip to content
Snippets Groups Projects
Commit f124134c authored by Clayton, Brandon Scott's avatar Clayton, Brandon Scott
Browse files

Merge branch 'requests' into 'main'

Interceptor

See merge request !542
parents ffa79991 b869c42a
No related branches found
No related tags found
1 merge request!542Interceptor
Pipeline #531301 passed with warnings
import {provideHttpClient} from '@angular/common/http'; import {provideHttpClient, withInterceptors} from '@angular/common/http';
import {enableProdMode, importProvidersFrom} from '@angular/core'; import {enableProdMode, importProvidersFrom} from '@angular/core';
import {bootstrapApplication, BrowserModule} from '@angular/platform-browser'; import {bootstrapApplication, BrowserModule} from '@angular/platform-browser';
import {provideAnimations} from '@angular/platform-browser/animations'; import {provideAnimations} from '@angular/platform-browser/animations';
...@@ -7,6 +7,7 @@ import {provideRouter} from '@angular/router'; ...@@ -7,6 +7,7 @@ import {provideRouter} from '@angular/router';
import {AppComponent} from './app/app.component'; import {AppComponent} from './app/app.component';
import {appRoutes} from './app/app.routes'; import {appRoutes} from './app/app.routes';
import {environment} from './environments/environment'; import {environment} from './environments/environment';
import {httpErrorInterceptor} from './shared/interceptors/http-error.interceptor';
if (environment.production) { if (environment.production) {
enableProdMode(); enableProdMode();
...@@ -17,6 +18,6 @@ bootstrapApplication(AppComponent, { ...@@ -17,6 +18,6 @@ bootstrapApplication(AppComponent, {
importProvidersFrom(BrowserModule), importProvidersFrom(BrowserModule),
provideRouter(appRoutes()), provideRouter(appRoutes()),
provideAnimations(), provideAnimations(),
provideHttpClient(), provideHttpClient(withInterceptors([httpErrorInterceptor])),
], ],
}).catch(err => console.error(err)); }).catch(err => console.error(err));
import {
HttpErrorResponse,
HttpEvent,
HttpHandlerFn,
HttpRequest,
} from '@angular/common/http';
import {catchError, Observable} from 'rxjs';
export function httpErrorInterceptor(
req: HttpRequest<unknown>,
next: HttpHandlerFn
): Observable<HttpEvent<unknown>> {
return next(req).pipe(
catchError(error => {
if (
error instanceof HttpErrorResponse &&
typeof error.error === 'string'
) {
throw new Error(error.error);
}
throw error;
})
);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment