Move error handler to error interceptor.

This commit is contained in:
Spencer Ofwiti
2021-03-21 16:11:05 +03:00
parent b5c9699e2c
commit f12e69b5df
4 changed files with 55 additions and 40 deletions

View File

@@ -6,8 +6,8 @@ import {LoggingService} from '@app/_services/logging.service';
import {MutableKeyStore, MutablePgpKeyStore} from '@app/_pgp';
import {ErrorDialogService} from '@app/_services/error-dialog.service';
import {tap} from 'rxjs/operators';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import {Observable, throwError} from 'rxjs';
import { HttpClient } from '@angular/common/http';
import {Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
@@ -154,7 +154,7 @@ export class AuthService {
return this.httpClient.get(`${environment.publicKeysUrl}`, {responseType: 'text'})
.pipe(tap(
data => { },
error => { this.handleError(error, 'Unable to load trusted public keys.'); }
error => { this.loggingService.sendErrorLevelMessage('Unable to load trusted public keys.', this, {error}); }
));
}
@@ -163,20 +163,4 @@ export class AuthService {
await this.mutableKeyStore.importPrivateKey(this.privateKey);
}
}
// TODO this is from the docs and for reference. Move it somewhere better.
private handleError(error: HttpErrorResponse, customMessage: string): Observable<any> {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong.
console.error(
`Backend returned code ${error.status}, ` +
`body was: ${JSON.stringify(error.error)}`);
}
// Return an observable with a user-facing error message.
return throwError(customMessage);
}
}