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 { 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 { Observable, of } from 'rxjs';
 | 
				
			||||||
import { tap } from 'rxjs/operators';
 | 
					import { catchError, tap } from 'rxjs/operators';
 | 
				
			||||||
import { TransferState, makeStateKey } from '@angular/platform-browser';
 | 
					import { TransferState, makeStateKey } from '@angular/platform-browser';
 | 
				
			||||||
import { isPlatformBrowser } from '@angular/common';
 | 
					import { isPlatformBrowser } from '@angular/common';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -36,7 +36,8 @@ export class HttpCacheInterceptor implements HttpInterceptor {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return next.handle(request)
 | 
					    return next.handle(request)
 | 
				
			||||||
      .pipe(tap((event: HttpEvent<any>) => {
 | 
					      .pipe(
 | 
				
			||||||
 | 
					        tap((event: HttpEvent<any>) => {
 | 
				
			||||||
          if (!this.isBrowser && event instanceof HttpResponse) {
 | 
					          if (!this.isBrowser && event instanceof HttpResponse) {
 | 
				
			||||||
            let keyId = request.url.split('/').slice(3).join('/');
 | 
					            let keyId = request.url.split('/').slice(3).join('/');
 | 
				
			||||||
            const headers = {};
 | 
					            const headers = {};
 | 
				
			||||||
@ -45,6 +46,31 @@ export class HttpCacheInterceptor implements HttpInterceptor {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            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