-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
HttpInterceptor, | ||
HttpErrorResponse, | ||
HTTP_INTERCEPTORS, | ||
} from '@angular/common/http'; | ||
import { catchError } from 'rxjs/operators'; | ||
import { throwError } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class ErrorInterceptor implements HttpInterceptor { | ||
intercept( | ||
req: import('@angular/common/http').HttpRequest<any>, | ||
next: import('@angular/common/http').HttpHandler | ||
): import('rxjs').Observable<import('@angular/common/http').HttpEvent<any>> { | ||
return next.handle(req).pipe( | ||
catchError((error) => { | ||
if (error.status === 401) { | ||
return throwError(error.status); | ||
} | ||
if (error instanceof HttpErrorResponse) { | ||
const applicationError = error.headers.get('Application-Error'); | ||
if (applicationError) { | ||
return throwError(applicationError); | ||
} | ||
|
||
const serverError = JSON.parse(error.error); | ||
let modalStateErrors = ''; | ||
|
||
if (serverError.errors && typeof serverError.errors === 'object') { | ||
for (const key in serverError.errors) { | ||
if (serverError.errors[key]) { | ||
modalStateErrors += serverError.errors[key] + '\n'; | ||
} | ||
} | ||
} else if (serverError[0]) { | ||
for (const index in serverError) { | ||
if (serverError[index]) { | ||
modalStateErrors += serverError[index].description; | ||
} | ||
} | ||
} | ||
|
||
return throwError(modalStateErrors || serverError || 'Server Error'); | ||
} | ||
}) | ||
); | ||
} | ||
} | ||
|
||
export const ErrorInterceptorProvider = { | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: ErrorInterceptor, | ||
multi: true, | ||
}; |