2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
type Context = {
|
|
|
|
|
type: string;
|
|
|
|
|
id: string;
|
|
|
|
|
}[];
|
2021-02-24 08:17:40 +01:00
|
|
|
|
2020-01-15 17:38:11 +01:00
|
|
|
export type Violation = {
|
|
|
|
|
path?: string;
|
2019-10-19 16:38:07 +02:00
|
|
|
message: string;
|
2020-01-15 17:38:11 +01:00
|
|
|
key?: string;
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
2021-02-24 08:17:40 +01:00
|
|
|
|
2020-11-10 08:33:22 +01:00
|
|
|
export type AdditionalMessage = {
|
|
|
|
|
key?: string;
|
|
|
|
|
message?: string;
|
|
|
|
|
};
|
2018-11-15 21:39:08 +01:00
|
|
|
|
|
|
|
|
export type BackendErrorContent = {
|
2019-10-19 16:38:07 +02:00
|
|
|
transactionId: string;
|
|
|
|
|
errorCode: string;
|
|
|
|
|
message: string;
|
|
|
|
|
url?: string;
|
|
|
|
|
context: Context;
|
|
|
|
|
violations: Violation[];
|
2020-11-10 08:33:22 +01:00
|
|
|
additionalMessages?: AdditionalMessage[];
|
2018-11-15 21:39:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export class BackendError extends Error {
|
|
|
|
|
transactionId: string;
|
|
|
|
|
errorCode: string;
|
2019-10-19 16:38:07 +02:00
|
|
|
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[];
|
2020-11-10 08:33:22 +01:00
|
|
|
additionalMessages?: AdditionalMessage[];
|
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;
|
2020-11-10 08:33:22 +01:00
|
|
|
this.additionalMessages = content.additionalMessages;
|
2018-11-15 21:39:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-25 17:40:53 +01:00
|
|
|
export class UnauthorizedError extends Error {
|
|
|
|
|
statusCode: number;
|
2021-03-16 16:13:43 +01:00
|
|
|
|
2019-02-25 17:40:53 +01:00
|
|
|
constructor(message: string, statusCode: number) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.statusCode = statusCode;
|
2018-11-15 21:39:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 10:02:46 +01:00
|
|
|
export class BadGatewayError extends Error {
|
|
|
|
|
statusCode: number;
|
|
|
|
|
|
|
|
|
|
constructor(message: string, statusCode: number) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.statusCode = statusCode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 16:13:43 +01:00
|
|
|
export class TokenExpiredError extends UnauthorizedError {}
|
|
|
|
|
|
2019-03-04 11:49:12 +01:00
|
|
|
export class ForbiddenError extends Error {
|
|
|
|
|
statusCode: number;
|
2021-03-16 16:13:43 +01:00
|
|
|
|
2019-03-04 11:49:12 +01:00
|
|
|
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) {
|
2019-10-20 16:59:02 +02:00
|
|
|
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) {
|
2019-10-20 16:59:02 +02:00
|
|
|
super(content, "ConflictError", statusCode);
|
2019-03-08 08:58:09 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 13:20:43 +02:00
|
|
|
export class MissingLinkError extends Error {
|
|
|
|
|
name = "MissingLinkError";
|
|
|
|
|
}
|
|
|
|
|
|
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:
|
2019-10-20 16:59:02 +02:00
|
|
|
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
|
|
|
}
|
2020-08-20 18:57:58 +02:00
|
|
|
|
|
|
|
|
export const TOKEN_EXPIRED_ERROR_CODE = "DDS8D8unr1";
|