Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added app module and http error interceptor
- Loading branch information
Showing
2 changed files
with
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}) | ||
) | ||
} | ||
} |