Add reject body interface.

This commit is contained in:
Spencer Ofwiti 2021-05-17 09:06:07 +03:00
parent ff17d3b02f
commit 27914d4a7d
3 changed files with 22 additions and 20 deletions

View File

@ -87,3 +87,10 @@ export class GlobalErrorHandler extends ErrorHandler {
return isWarning;
}
}
export function rejectBody(error): { status: any; statusText: any } {
return {
status: error.status,
statusText: error.statusText,
};
}

View File

@ -1,3 +1,5 @@
import { rejectBody } from '@app/_helpers/global-error-handler';
function HttpGetter(): void {}
HttpGetter.prototype.get = (filename) =>
@ -6,7 +8,7 @@ HttpGetter.prototype.get = (filename) =>
if (response.ok) {
resolve(response.json());
} else {
reject(`failed with status ${response.status} : ${response.statusText}`);
reject(rejectBody(response));
}
return;
});

View File

@ -6,7 +6,7 @@ import { LoggingService } from '@app/_services/logging.service';
import { MutableKeyStore, MutablePgpKeyStore } from '@app/_pgp';
import { ErrorDialogService } from '@app/_services/error-dialog.service';
import { HttpClient } from '@angular/common/http';
import { HttpError } from '@app/_helpers/global-error-handler';
import { HttpError, rejectBody } from '@app/_helpers/global-error-handler';
@Injectable({
providedIn: 'root',
@ -50,10 +50,7 @@ export class AuthService {
};
fetch(environment.cicMetaUrl, options).then((response) => {
if (response.status === 401) {
return reject({
status: response.status,
statusText: response.statusText,
});
return reject(rejectBody(response));
}
return resolve(true);
});
@ -73,10 +70,7 @@ export class AuthService {
};
fetch(environment.cicMetaUrl, options).then((response) => {
if (response.status === 401) {
return reject({
status: response.status,
statusText: response.statusText,
});
return reject(rejectBody(response));
}
this.sessionToken = response.headers.get('Token');
sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), this.sessionToken);
@ -94,10 +88,7 @@ export class AuthService {
return resolve(hobaParseChallengeHeader(authHeader));
}
if (!response.ok) {
return reject({
status: response.status,
statusText: response.statusText,
});
return reject(rejectBody(response));
}
});
});
@ -206,12 +197,14 @@ export class AuthService {
}
async getPublicKeys(): Promise<any> {
return await fetch(environment.publicKeysUrl).then((res) => {
if (!res.ok) {
// TODO does angular recommend an error interface?
throw Error(`${res.statusText} - ${res.status}`);
}
return res.text();
return new Promise((resolve, reject) => {
fetch(environment.publicKeysUrl).then((res) => {
if (!res.ok) {
// TODO does angular recommend an error interface?
return reject(rejectBody(res));
}
return resolve(res.text());
});
});
}