mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-18 15:47:57 +01:00
Fix reporting of server-side errors in "bw sync". (#6855)
Co-authored-by: SmithThe4th <gsmith@bitwarden.com>
This commit is contained in:
parent
46e2e0233b
commit
7a5f3b2dd4
@ -1,21 +1,36 @@
|
|||||||
// FIXME: Update this file to be type safe and remove this and next line
|
// FIXME: Update this file to be type safe and remove this and next line
|
||||||
// @ts-strict-ignore
|
// @ts-strict-ignore
|
||||||
|
import { ErrorResponse } from "@bitwarden/common/models/response/error.response";
|
||||||
|
|
||||||
import { BaseResponse } from "./response/base.response";
|
import { BaseResponse } from "./response/base.response";
|
||||||
|
|
||||||
|
function getErrorMessage(error: unknown): string {
|
||||||
|
if (typeof error === "string") {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
if (error instanceof ErrorResponse) {
|
||||||
|
const message = error.getSingleMessage();
|
||||||
|
if (message) {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (error instanceof Error) {
|
||||||
|
return String(error);
|
||||||
|
}
|
||||||
|
if (error) {
|
||||||
|
const errorWithMessage: { message?: unknown } = error; // To placate TypeScript.
|
||||||
|
if (errorWithMessage.message && typeof errorWithMessage.message === "string") {
|
||||||
|
return errorWithMessage.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return JSON.stringify(error);
|
||||||
|
}
|
||||||
|
|
||||||
export class Response {
|
export class Response {
|
||||||
static error(error: any, data?: any): Response {
|
static error(error: any, data?: any): Response {
|
||||||
const res = new Response();
|
const res = new Response();
|
||||||
res.success = false;
|
res.success = false;
|
||||||
if (typeof error === "string") {
|
res.message = getErrorMessage(error);
|
||||||
res.message = error;
|
|
||||||
} else {
|
|
||||||
res.message =
|
|
||||||
error.message != null
|
|
||||||
? error.message
|
|
||||||
: error.toString() === "[object Object]"
|
|
||||||
? JSON.stringify(error)
|
|
||||||
: error.toString();
|
|
||||||
}
|
|
||||||
res.data = data;
|
res.data = data;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,9 @@ export class SyncCommand {
|
|||||||
const res = new MessageResponse("Syncing complete.", null);
|
const res = new MessageResponse("Syncing complete.", null);
|
||||||
return Response.success(res);
|
return Response.success(res);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return Response.error("Syncing failed: " + e.toString());
|
const response = Response.error(e);
|
||||||
|
response.message = "Syncing failed: " + response.message;
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1948,7 +1948,6 @@ export class ApiService implements ApiServiceAbstraction {
|
|||||||
responseJson.error === "invalid_grant")
|
responseJson.error === "invalid_grant")
|
||||||
) {
|
) {
|
||||||
await this.logoutCallback("invalidGrantError");
|
await this.logoutCallback("invalidGrantError");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user