Files
SCM-Manager/scm-ui/ui-components/src/errors.ts

82 lines
2.0 KiB
TypeScript
Raw Normal View History

type Context = {
type: string;
id: string;
}[];
export type Violation = {
path?: string;
message: string;
key?: string;
};
2018-11-15 21:39:08 +01:00
export type BackendErrorContent = {
transactionId: string;
errorCode: string;
message: string;
url?: string;
context: Context;
violations: Violation[];
2018-11-15 21:39:08 +01:00
};
export class BackendError extends Error {
transactionId: string;
errorCode: string;
url: string | null | undefined;
2018-11-15 21:39:08 +01:00
context: Context = [];
statusCode: number;
2019-03-04 11:09:12 +01:00
violations: Violation[];
2018-11-15 21:39:08 +01:00
constructor(content: BackendErrorContent, name: string, statusCode: number) {
super(content.message);
this.name = name;
this.transactionId = content.transactionId;
this.errorCode = content.errorCode;
this.url = content.url;
this.context = content.context;
this.statusCode = statusCode;
2019-03-04 11:09:12 +01:00
this.violations = content.violations;
2018-11-15 21:39:08 +01:00
}
}
2019-02-25 17:40:53 +01:00
export class UnauthorizedError extends Error {
statusCode: number;
constructor(message: string, statusCode: number) {
super(message);
this.statusCode = statusCode;
2018-11-15 21:39:08 +01:00
}
}
2019-03-04 11:49:12 +01:00
export class ForbiddenError extends Error {
statusCode: number;
constructor(message: string, statusCode: number) {
super(message);
this.statusCode = statusCode;
}
}
2018-11-15 21:39:08 +01:00
export class NotFoundError extends BackendError {
constructor(content: BackendErrorContent, statusCode: number) {
super(content, "NotFoundError", statusCode);
2018-11-15 21:39:08 +01:00
}
}
2019-03-08 08:58:09 +01:00
export class ConflictError extends BackendError {
constructor(content: BackendErrorContent, statusCode: number) {
super(content, "ConflictError", statusCode);
2019-03-08 08:58:09 +01:00
}
}
2019-10-21 10:57:56 +02:00
export function createBackendError(content: BackendErrorContent, statusCode: number) {
2018-11-15 21:39:08 +01:00
switch (statusCode) {
case 404:
return new NotFoundError(content, statusCode);
2019-03-08 08:58:09 +01:00
case 409:
return new ConflictError(content, statusCode);
2018-11-15 21:39:08 +01:00
default:
return new BackendError(content, "BackendError", statusCode);
2018-11-15 21:39:08 +01:00
}
}
2019-02-25 17:40:53 +01:00
export function isBackendError(response: Response) {
2019-10-21 10:57:56 +02:00
return response.headers.get("Content-Type") === "application/vnd.scmm-error+json;v=2";
2019-02-25 17:40:53 +01:00
}