Escalate errors to higher level handlers.

This commit is contained in:
Spencer Ofwiti 2021-04-21 12:37:40 +03:00
parent 53ed56460c
commit 63cff19bae
2 changed files with 32 additions and 11 deletions

View File

@ -45,10 +45,13 @@ export class AuthService {
};
fetch(environment.cicMetaUrl, options).then(response => {
if (response.status === 401) {
return Promise.reject({ statusText: response.statusText });
return Promise.reject({
status: response.status,
statusText: response.statusText
});
}
return;
}).catch(error => this.loggingService.sendErrorLevelMessage('Login rejected', this, {error}));
});
}
sendResponse(hobaResponseEncoded): void {
@ -62,13 +65,16 @@ export class AuthService {
};
fetch(environment.cicMetaUrl, options).then(response => {
if (response.status === 401) {
return Promise.reject({ statusText: response.statusText });
return Promise.reject({
status: response.status,
statusText: response.statusText
});
}
this.sessionToken = response.headers.get('Token');
sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), this.sessionToken);
this.setState('Click button to log in');
return;
}).catch(error => this.loggingService.sendErrorLevelMessage('Login rejected', this, {error}));
});
}
async getChallenge(): Promise<any> {
@ -77,7 +83,13 @@ export class AuthService {
const authHeader = response.headers.get('WWW-Authenticate');
return hobaParseChallengeHeader(authHeader);
}
}).catch(error => this.loggingService.sendErrorLevelMessage('Fetching challenge failed', this, {error}));
if (!response.ok) {
return Promise.reject({
status: response.status,
statusText: response.statusText
});
}
});
}
async passwordLogin(password: string): Promise<boolean> {
@ -85,8 +97,9 @@ export class AuthService {
const o = await this.getChallenge();
await this.loginResponse(o, password);
return true;
} catch (e) {
this.loggingService.sendErrorLevelMessage('Login challenge failed', this, {error: e});
} catch (error) {
this.loggingService.sendErrorLevelMessage(`Login challenge failed: Error ${error.status} - ${error.statusText}`,
this, {error});
}
return false;
}
@ -96,8 +109,9 @@ export class AuthService {
try {
this.getWithToken();
return true;
} catch (e) {
this.loggingService.sendErrorLevelMessage('Login token failed', this, {error: e});
} catch (error) {
this.loggingService.sendErrorLevelMessage(`Login token failed: Error ${error.status} - ${error.statusText}`,
this, {error});
}
}
return false;
@ -110,6 +124,10 @@ export class AuthService {
this.sendResponse(r);
} catch (error) {
this.errorDialogService.openDialog({message: 'Incorrect key passphrase.'});
return Promise.reject({
status: error.status,
statusText: error.statusText
});
}
}

View File

@ -1,4 +1,4 @@
import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core';
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {CustomErrorStateMatcher} from '@app/_helpers';
import {AuthService} from '@app/_services';
@ -22,7 +22,8 @@ export class AuthComponent implements OnInit {
constructor(
private authService: AuthService,
private formBuilder: FormBuilder,
private router: Router
private router: Router,
private cdr: ChangeDetectorRef,
) { }
async ngOnInit(): Promise<void> {
@ -57,6 +58,7 @@ export class AuthComponent implements OnInit {
this.passwordInput();
}
this.keyFormLoading = false;
this.cdr.detectChanges();
}
async onPasswordInput(): Promise<void> {
@ -70,6 +72,7 @@ export class AuthComponent implements OnInit {
this.loginView();
}
this.passwordFormLoading = false;
this.cdr.detectChanges();
}
login(): void {