1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 08:43:33 +00:00
Files
browser/libs/common/src/models/response/error.response.ts
Matt Gibson 78248db590 Platform/pm 19/platform team file moves (#5460)
* Rename service-factory folder

* Move cryptographic service factories

* Move crypto models

* Move crypto services

* Move domain base class

* Platform code owners

* Move desktop log services

* Move log files

* Establish component library ownership

* Move background listeners

* Move background background

* Move localization to Platform

* Move browser alarms to Platform

* Move browser state to Platform

* Move CLI state to Platform

* Move Desktop native concerns to Platform

* Move flag and misc to Platform

* Lint fixes

* Move electron state to platform

* Move web state to Platform

* Move lib state to Platform

* Fix broken tests

* Rename interface to idiomatic TS

* `npm run prettier` 🤖

* Resolve review feedback

* Set platform as owners of web core and shared

* Expand moved services

* Fix test types

---------

Co-authored-by: Hinton <hinton@users.noreply.github.com>
2023-06-06 15:34:53 -05:00

73 lines
2.2 KiB
TypeScript

import { Utils } from "../../platform/misc/utils";
import { BaseResponse } from "./base.response";
export class ErrorResponse extends BaseResponse {
message: string;
validationErrors: { [key: string]: string[] };
statusCode: number;
captchaRequired: boolean;
captchaSiteKey: string;
constructor(response: any, status: number, identityResponse?: boolean) {
super(response);
let errorModel = null;
if (response != null) {
const responseErrorModel = this.getResponseProperty("ErrorModel");
if (responseErrorModel && identityResponse) {
errorModel = responseErrorModel;
} else {
errorModel = response;
}
}
if (status === 429) {
this.message = "Rate limit exceeded. Try again later.";
} else if (errorModel) {
this.message = this.getResponseProperty("Message", errorModel);
this.validationErrors = this.getResponseProperty("ValidationErrors", errorModel);
this.captchaSiteKey = this.validationErrors?.HCaptcha_SiteKey?.[0];
this.captchaRequired = !Utils.isNullOrWhitespace(this.captchaSiteKey);
}
this.statusCode = status;
}
getSingleMessage(): string {
if (this.validationErrors == null) {
return this.message;
}
for (const key in this.validationErrors) {
// eslint-disable-next-line
if (!this.validationErrors.hasOwnProperty(key)) {
continue;
}
if (this.validationErrors[key].length) {
return this.validationErrors[key][0];
}
}
return this.message;
}
getAllMessages(): string[] {
const messages: string[] = [];
if (this.validationErrors == null) {
return messages;
}
for (const key in this.validationErrors) {
// eslint-disable-next-line
if (!this.validationErrors.hasOwnProperty(key)) {
continue;
}
this.validationErrors[key].forEach((item: string) => {
let prefix = "";
if (key.indexOf("[") > -1 && key.indexOf("]") > -1) {
const lastSep = key.lastIndexOf(".");
prefix = key.substr(0, lastSep > -1 ? lastSep : key.length) + ": ";
}
messages.push(prefix + item);
});
}
return messages;
}
}