Extend http interceptor to normalize errors
This commit is contained in:
		
							parent
							
								
									484e032775
								
							
						
					
					
						commit
						13fb75fec3
					
				| @ -1,7 +1,7 @@ | ||||
| import { Inject, Injectable, PLATFORM_ID } from '@angular/core'; | ||||
| import { HttpInterceptor, HttpEvent, HttpRequest, HttpHandler, HttpResponse, HttpHeaders } from '@angular/common/http'; | ||||
| import { HttpInterceptor, HttpEvent, HttpRequest, HttpHandler, HttpResponse, HttpErrorResponse, HttpHeaders } from '@angular/common/http'; | ||||
| import { Observable, of } from 'rxjs'; | ||||
| import { tap } from 'rxjs/operators'; | ||||
| import { catchError, tap } from 'rxjs/operators'; | ||||
| import { TransferState, makeStateKey } from '@angular/platform-browser'; | ||||
| import { isPlatformBrowser } from '@angular/common'; | ||||
| 
 | ||||
| @ -36,15 +36,41 @@ export class HttpCacheInterceptor implements HttpInterceptor { | ||||
|     } | ||||
| 
 | ||||
|     return next.handle(request) | ||||
|       .pipe(tap((event: HttpEvent<any>) => { | ||||
|         if (!this.isBrowser && event instanceof HttpResponse) { | ||||
|           let keyId = request.url.split('/').slice(3).join('/'); | ||||
|           const headers = {}; | ||||
|           for (const k of event.headers.keys()) { | ||||
|             headers[k] = event.headers.getAll(k); | ||||
|       .pipe( | ||||
|         tap((event: HttpEvent<any>) => { | ||||
|           if (!this.isBrowser && event instanceof HttpResponse) { | ||||
|             let keyId = request.url.split('/').slice(3).join('/'); | ||||
|             const headers = {}; | ||||
|             for (const k of event.headers.keys()) { | ||||
|               headers[k] = event.headers.getAll(k); | ||||
|             } | ||||
|             this.transferState.set<any>(makeStateKey('/' + keyId), { response: event, headers }); | ||||
|           } | ||||
|           this.transferState.set<any>(makeStateKey('/' + keyId), { response: event, headers }); | ||||
|         } | ||||
|       })); | ||||
|         }), | ||||
|         catchError((e) => { | ||||
|           if (e instanceof HttpErrorResponse) { | ||||
|             if (e.status === 0) { | ||||
|               throw new HttpErrorResponse({ | ||||
|                 error: 'Unknown error', | ||||
|                 headers: e.headers, | ||||
|                 status: 0, | ||||
|                 statusText: 'Unknown error', | ||||
|                 url: e.url, | ||||
|               }); | ||||
|             } else { | ||||
|               throw e; | ||||
|             } | ||||
|           } else { | ||||
|             const msg = e?.['message'] || 'Unknown error'; | ||||
|             throw new HttpErrorResponse({ | ||||
|               error: msg, | ||||
|               headers: new HttpHeaders(), | ||||
|               status: 0, | ||||
|               statusText: msg, | ||||
|               url: '', | ||||
|             }); | ||||
|           } | ||||
|         }) | ||||
|       ); | ||||
|   } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user