diff --git a/src/models/response/errorResponse.ts b/src/models/response/errorResponse.ts index 03dfda14b06..4d976932cd2 100644 --- a/src/models/response/errorResponse.ts +++ b/src/models/response/errorResponse.ts @@ -9,8 +9,6 @@ class ErrorResponse { errorModel = response.ErrorModel; } else if (response) { errorModel = response; - //} else if (response.responseText && response.responseText.indexOf('{') === 0) { - // errorModel = JSON.parse(response.responseText); } if (errorModel) { @@ -19,6 +17,20 @@ class ErrorResponse { } this.statusCode = status; } + + getSingleMessage(): string { + if (this.validationErrors) { + for (const key in this.validationErrors) { + if (!this.validationErrors.hasOwnProperty(key)) { + continue; + } + if (this.validationErrors[key].length) { + return this.validationErrors[key][0]; + } + } + } + return this.message; + } } export { ErrorResponse }; diff --git a/src/services/api.service.ts b/src/services/api.service.ts index f11a58968db..13d992fe9c1 100644 --- a/src/services/api.service.ts +++ b/src/services/api.service.ts @@ -366,8 +366,7 @@ export default class ApiService { // Helpers private async handleError(response: Response, tokenError: boolean): Promise { - if (response != null && (tokenError && response.status === 400) || - response.status === 401 || response.status === 403) { + if ((tokenError && response.status === 400) || response.status === 401 || response.status === 403) { if (this.logoutCallback) { this.logoutCallback(true); } else { diff --git a/src/services/cipher.service.ts b/src/services/cipher.service.ts index 99a48a51dd2..f51af0e70ba 100644 --- a/src/services/cipher.service.ts +++ b/src/services/cipher.service.ts @@ -68,7 +68,7 @@ export default class CipherService { decryptedCipherCache: any[]; constructor(private cryptoService: CryptoService, private userService: UserService, - private settingsService: SettingsService, private apiService: ApiService) { + private settingsService: SettingsService, private apiService: ApiService) { } clearCache(): void { @@ -312,12 +312,19 @@ export default class CipherService { const blob = new Blob([encData], { type: 'application/octet-stream' }); fd.append('data', blob, encFileName.encryptedString); - const response = await self.apiService.postCipherAttachment(cipher.id, fd); - // TODO: handle error response + let response: CipherResponse; + try { + response = await self.apiService.postCipherAttachment(cipher.id, fd); + } catch (e) { + reject((e as ErrorResponse).getSingleMessage()); + return; + } + const userId = await self.userService.getUserId(); const data = new CipherData(response, userId); this.upsert(data); resolve(new Cipher(data)); + }; reader.onerror = (evt) => { @@ -404,13 +411,14 @@ export default class CipherService { } async deleteAttachmentWithServer(id: string, attachmentId: string): Promise { - await this.apiService.deleteCipherAttachment(id, attachmentId); + try { + await this.apiService.deleteCipherAttachment(id, attachmentId); + } catch (e) { + return Promise.reject((e as ErrorResponse).getSingleMessage()); + } await this.deleteAttachment(id, attachmentId); - // TODO: handle error } - // TODO: remove in favor of static refs - sortCiphersByLastUsed(a: any, b: any): number { return CipherService.sortCiphersByLastUsed(a, b); } @@ -499,20 +507,4 @@ export default class CipherService { throw new Error('Unknown cipher type.'); } } - - private handleErrorMessage(error: ErrorResponse, reject: Function): void { - if (error.validationErrors) { - for (const key in error.validationErrors) { - if (!error.validationErrors.hasOwnProperty(key)) { - continue; - } - if (error.validationErrors[key].length) { - reject(error.validationErrors[key][0]); - return; - } - } - } - reject(error.message); - return; - } } diff --git a/src/services/passwordGeneration.service.ts b/src/services/passwordGeneration.service.ts index b724c97b43e..0375d3d1fa3 100644 --- a/src/services/passwordGeneration.service.ts +++ b/src/services/passwordGeneration.service.ts @@ -156,7 +156,6 @@ export default class PasswordGenerationService { }); } - // TODO: remove in favor of static generatePassword(options: any) { return PasswordGenerationService.generatePassword(options); }