1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-02 09:43:29 +00:00
Files
browser/apps/cli/src/models/response.ts
Jason Ng 327aed9763 [PM-11162] Assign To Collections Permission Update (#11367)
Only users with Manage/Edit permissions will be allowed to Assign To Collections. If the user has Can Edit Except Password the collections dropdown will be disabled.
---------

Co-authored-by: Matt Bishop <mbishop@bitwarden.com>
Co-authored-by: kejaeger <138028972+kejaeger@users.noreply.github.com>
2025-02-04 15:44:59 -05:00

72 lines
1.8 KiB
TypeScript

// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import { ErrorResponse } from "@bitwarden/common/models/response/error.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 {
static error(error: any, data?: any): Response {
const res = new Response();
res.success = false;
res.message = getErrorMessage(error);
res.data = data;
return res;
}
static notFound(): Response {
return Response.error("Not found.");
}
static noEditPermission(): Response {
return Response.error("You do not have permission to edit this item");
}
static badRequest(message: string): Response {
return Response.error(message);
}
static multipleResults(ids: string[]): Response {
let msg =
"More than one result was found. Try getting a specific object by `id` instead. " +
"The following objects were found:";
ids.forEach((id) => {
msg += "\n" + id;
});
return Response.error(msg, ids);
}
static success(data?: BaseResponse): Response {
const res = new Response();
res.success = true;
res.data = data;
return res;
}
success: boolean;
message: string;
errorCode: number;
data: BaseResponse;
}