Skip to content

Commit

Permalink
Add front-end global exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
marvac committed Jan 9, 2019
1 parent 08cd142 commit 78b40b0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Client/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NavMenuComponent } from './components/nav-menu/nav-menu.component';
import { AuthService } from './services/auth.service';
import { HomeComponent } from './components/home/home.component';
import { RegisterComponent } from './components/register/register.component';
import { ErrorInterceptorProvider } from './services/error.interceptor';

@NgModule({
declarations: [
Expand All @@ -22,7 +23,8 @@ import { RegisterComponent } from './components/register/register.component';
FormsModule
],
providers: [
AuthService
AuthService,
ErrorInterceptorProvider
],
bootstrap: [AppComponent]
})
Expand Down
2 changes: 1 addition & 1 deletion Client/src/app/components/nav-menu/nav-menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class NavMenuComponent implements OnInit {
this.authService.login(this.model).subscribe(next => {
console.log('logged in')
}, error => {
console.log('login failed')
console.log(error)
});
}

Expand Down
45 changes: 45 additions & 0 deletions Client/src/app/services/error.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Injectable } from "@angular/core";
import { HttpInterceptor, HttpHandler, HttpEvent, HttpErrorResponse, HttpRequest, HTTP_INTERCEPTORS } from "@angular/common/http";
import { Observable, throwError } from "rxjs";
import { catchError } from "rxjs/operators";

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
catchError(error => {
if (error instanceof HttpErrorResponse) {

if (error.status === 401) {
return throwError(error.statusText);
}

const applicationError = error.headers.get('Application-Error');

if (applicationError) {
return throwError(applicationError);
}

const serverError = error.error;
let modelStateErrors = '';

if (serverError && typeof serverError === 'object') {
for (const key in serverError) {
if (serverError[key]) {
modelStateErrors += `${serverError[key]}\n`;
}
}

return throwError(modelStateErrors || serverError || 'Server Error');
}
}
}));
}

}

export const ErrorInterceptorProvider = {
provide: HTTP_INTERCEPTORS,
useClass: ErrorInterceptor,
multi: true
}
1 change: 0 additions & 1 deletion Friendster.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
</PropertyGroup>

<ItemGroup>
<Folder Include="Client\src\app\services\" />
<Folder Include="wwwroot\" />
</ItemGroup>

Expand Down

0 comments on commit 78b40b0

Please sign in to comment.