handle server errors for attachment save
This commit is contained in:
parent
d9f6607073
commit
a166dc65b1
|
@ -9,8 +9,6 @@ class ErrorResponse {
|
||||||
errorModel = response.ErrorModel;
|
errorModel = response.ErrorModel;
|
||||||
} else if (response) {
|
} else if (response) {
|
||||||
errorModel = response;
|
errorModel = response;
|
||||||
//} else if (response.responseText && response.responseText.indexOf('{') === 0) {
|
|
||||||
// errorModel = JSON.parse(response.responseText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errorModel) {
|
if (errorModel) {
|
||||||
|
@ -19,6 +17,20 @@ class ErrorResponse {
|
||||||
}
|
}
|
||||||
this.statusCode = status;
|
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 };
|
export { ErrorResponse };
|
||||||
|
|
|
@ -366,8 +366,7 @@ export default class ApiService {
|
||||||
// Helpers
|
// Helpers
|
||||||
|
|
||||||
private async handleError(response: Response, tokenError: boolean): Promise<ErrorResponse> {
|
private async handleError(response: Response, tokenError: boolean): Promise<ErrorResponse> {
|
||||||
if (response != null && (tokenError && response.status === 400) ||
|
if ((tokenError && response.status === 400) || response.status === 401 || response.status === 403) {
|
||||||
response.status === 401 || response.status === 403) {
|
|
||||||
if (this.logoutCallback) {
|
if (this.logoutCallback) {
|
||||||
this.logoutCallback(true);
|
this.logoutCallback(true);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -312,12 +312,19 @@ export default class CipherService {
|
||||||
const blob = new Blob([encData], { type: 'application/octet-stream' });
|
const blob = new Blob([encData], { type: 'application/octet-stream' });
|
||||||
fd.append('data', blob, encFileName.encryptedString);
|
fd.append('data', blob, encFileName.encryptedString);
|
||||||
|
|
||||||
const response = await self.apiService.postCipherAttachment(cipher.id, fd);
|
let response: CipherResponse;
|
||||||
// TODO: handle error response
|
try {
|
||||||
|
response = await self.apiService.postCipherAttachment(cipher.id, fd);
|
||||||
|
} catch (e) {
|
||||||
|
reject((e as ErrorResponse).getSingleMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const userId = await self.userService.getUserId();
|
const userId = await self.userService.getUserId();
|
||||||
const data = new CipherData(response, userId);
|
const data = new CipherData(response, userId);
|
||||||
this.upsert(data);
|
this.upsert(data);
|
||||||
resolve(new Cipher(data));
|
resolve(new Cipher(data));
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
reader.onerror = (evt) => {
|
reader.onerror = (evt) => {
|
||||||
|
@ -404,12 +411,13 @@ export default class CipherService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteAttachmentWithServer(id: string, attachmentId: string): Promise<void> {
|
async deleteAttachmentWithServer(id: string, attachmentId: string): Promise<void> {
|
||||||
|
try {
|
||||||
await this.apiService.deleteCipherAttachment(id, attachmentId);
|
await this.apiService.deleteCipherAttachment(id, attachmentId);
|
||||||
await this.deleteAttachment(id, attachmentId);
|
} catch (e) {
|
||||||
// TODO: handle error
|
return Promise.reject((e as ErrorResponse).getSingleMessage());
|
||||||
|
}
|
||||||
|
await this.deleteAttachment(id, attachmentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove in favor of static refs
|
|
||||||
|
|
||||||
sortCiphersByLastUsed(a: any, b: any): number {
|
sortCiphersByLastUsed(a: any, b: any): number {
|
||||||
return CipherService.sortCiphersByLastUsed(a, b);
|
return CipherService.sortCiphersByLastUsed(a, b);
|
||||||
|
@ -499,20 +507,4 @@ export default class CipherService {
|
||||||
throw new Error('Unknown cipher type.');
|
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) {
|
generatePassword(options: any) {
|
||||||
return PasswordGenerationService.generatePassword(options);
|
return PasswordGenerationService.generatePassword(options);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue