mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-27 12:36:14 +01:00
handle server errors for attachment save
This commit is contained in:
parent
d9f6607073
commit
a166dc65b1
@ -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 };
|
||||
|
@ -366,8 +366,7 @@ export default class ApiService {
|
||||
// Helpers
|
||||
|
||||
private async handleError(response: Response, tokenError: boolean): Promise<ErrorResponse> {
|
||||
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 {
|
||||
|
@ -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<void> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -156,7 +156,6 @@ export default class PasswordGenerationService {
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: remove in favor of static
|
||||
generatePassword(options: any) {
|
||||
return PasswordGenerationService.generatePassword(options);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user