diff --git a/projects/nshmp-apps/src/main.ts b/projects/nshmp-apps/src/main.ts index c5e4a2adcfeee9b74e88dd43364ccf4e1f9951cf..2c37f0ca79563f79857fb8e56e0a8753d4d1df1f 100644 --- a/projects/nshmp-apps/src/main.ts +++ b/projects/nshmp-apps/src/main.ts @@ -1,4 +1,4 @@ -import {provideHttpClient} from '@angular/common/http'; +import {provideHttpClient, withInterceptors} from '@angular/common/http'; import {enableProdMode, importProvidersFrom} from '@angular/core'; import {bootstrapApplication, BrowserModule} from '@angular/platform-browser'; import {provideAnimations} from '@angular/platform-browser/animations'; @@ -7,6 +7,7 @@ import {provideRouter} from '@angular/router'; import {AppComponent} from './app/app.component'; import {appRoutes} from './app/app.routes'; import {environment} from './environments/environment'; +import {tooManyRequestsInterceptor} from './shared/interceptors/too-many-requests.interceptor'; if (environment.production) { enableProdMode(); @@ -17,6 +18,6 @@ bootstrapApplication(AppComponent, { importProvidersFrom(BrowserModule), provideRouter(appRoutes()), provideAnimations(), - provideHttpClient(), + provideHttpClient(withInterceptors([tooManyRequestsInterceptor])), ], }).catch(err => console.error(err)); diff --git a/projects/nshmp-apps/src/shared/interceptors/too-many-requests.interceptor.ts b/projects/nshmp-apps/src/shared/interceptors/too-many-requests.interceptor.ts new file mode 100644 index 0000000000000000000000000000000000000000..a843e559998d550e1bd5f4a21d614bff9d86a85d --- /dev/null +++ b/projects/nshmp-apps/src/shared/interceptors/too-many-requests.interceptor.ts @@ -0,0 +1,28 @@ +import { + HttpErrorResponse, + HttpEvent, + HttpHandlerFn, + HttpRequest, +} from '@angular/common/http'; +import {catchError, Observable} from 'rxjs'; + +export function tooManyRequestsInterceptor( + req: HttpRequest<unknown>, + next: HttpHandlerFn +): Observable<HttpEvent<unknown>> { + return next(req).pipe( + catchError(error => { + console.log(error); + + if (error instanceof HttpErrorResponse) { + console.log(error.status); + + if (error.status === 429) { + console.log(error.statusText); + } + } + + throw error; + }) + ); +}