mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-06 05:25:44 +01:00
improve error handling in the ui
This commit is contained in:
53
scm-ui-components/packages/ui-components/src/errors.js
Normal file
53
scm-ui-components/packages/ui-components/src/errors.js
Normal file
@@ -0,0 +1,53 @@
|
||||
// @flow
|
||||
type Context = {type: string, id: string}[];
|
||||
|
||||
export type BackendErrorContent = {
|
||||
transactionId: string,
|
||||
errorCode: string,
|
||||
message: string,
|
||||
url?: string,
|
||||
context: Context
|
||||
};
|
||||
|
||||
export class BackendError extends Error {
|
||||
|
||||
transactionId: string;
|
||||
errorCode: string;
|
||||
url: ?string;
|
||||
context: Context = [];
|
||||
statusCode: number;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class UnauthorizedError extends BackendError {
|
||||
constructor(content: BackendErrorContent, statusCode: number) {
|
||||
super(content, "UnauthorizedError", statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
export class NotFoundError extends BackendError {
|
||||
constructor(content: BackendErrorContent, statusCode: number) {
|
||||
super(content, "NotFoundError", statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
export function createBackendError(content: BackendErrorContent, statusCode: number) {
|
||||
switch (statusCode) {
|
||||
case 403:
|
||||
return new UnauthorizedError(content, statusCode);
|
||||
case 404:
|
||||
return new NotFoundError(content, statusCode);
|
||||
default:
|
||||
return new BackendError(content, "BackendError", statusCode);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user