Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added app module and http error interceptor
  • Loading branch information
ema14006 committed Mar 10, 2019
1 parent c415323 commit d710fe5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/app/app.module.ts
@@ -1,7 +1,7 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';

import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
Expand All @@ -14,6 +14,7 @@ import { FrontpageComponent } from './frontpage/frontpage.component';
import { GeneratorSearchComponent } from './generator-search/generator-search.component';
import { SignUpComponent } from './sign-up/sign-up.component';
import { VisualizeAllComponent } from './visualize-all/visualize-all.component';
import { HttpErrorInterceptor } from './http-error.interceptor';

@NgModule({
declarations: [
Expand Down Expand Up @@ -46,7 +47,13 @@ import { VisualizeAllComponent } from './visualize-all/visualize-all.component';
]),

],
providers: [],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: HttpErrorInterceptor,
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
32 changes: 32 additions & 0 deletions src/app/http-error.interceptor.ts
@@ -0,0 +1,32 @@
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpResponse,
HttpErrorResponse
} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { retry, catchError } from 'rxjs/operators';

export class HttpErrorInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request)
.pipe(
retry(1),
catchError((error: HttpErrorResponse) => {
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
// client-side error
errorMessage = `Error: ${error.error.message}`;
} else {
// server-side error
//console.log("poop");
errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
}

return throwError(errorMessage);
})
)
}
}

0 comments on commit d710fe5

Please sign in to comment.