Set this.name in all error classes

Signed-off-by: Mihailo Vlajkovic <mihailovlajkovic98@gmail.com>
This commit is contained in:
Mihailo Vlajkovic
2023-06-02 18:51:40 +02:00
committed by Fredrik Adelöw
parent e205b3e6ed
commit a95b7dfa96
3 changed files with 20 additions and 12 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
'@backstage/errors': patch
---
Added a new parameter "name" to "CustomErrorBase" and updated all the existing extensions to use it
Set `this.name` in all error classes that extend `CustomErrorBase` class to their actual name
@@ -19,7 +19,6 @@ import { isError } from './assertion';
/**
* A base class that custom Error classes can inherit from.
* @param name This is useful when you extend this base class to pass a custom name that will be included in the error message
*
* @public
* @example
@@ -39,7 +38,7 @@ export class CustomErrorBase extends Error {
*/
readonly cause?: Error | undefined;
constructor(message?: string, cause?: Error | unknown, name?: string) {
constructor(message?: string, cause?: Error | unknown) {
let fullMessage = message;
if (cause !== undefined) {
const causeStr = stringifyError(cause);
@@ -54,7 +53,7 @@ export class CustomErrorBase extends Error {
Error.captureStackTrace?.(this, this.constructor);
this.name = name || 'CustomErrorBase';
this.name = 'CustomErrorBase';
this.cause = isError(cause) ? cause : undefined;
}
}
+17 -8
View File
@@ -35,7 +35,8 @@ import { CustomErrorBase } from './CustomErrorBase';
*/
export class InputError extends CustomErrorBase {
constructor(message?: string, cause?: Error | unknown) {
super(message, cause, 'InputError');
super(message, cause);
this.name = 'InputError';
}
}
@@ -46,7 +47,8 @@ export class InputError extends CustomErrorBase {
*/
export class AuthenticationError extends CustomErrorBase {
constructor(message?: string, cause?: Error | unknown) {
super(message, cause, 'AuthenticationError');
super(message, cause);
this.name = 'AuthenticationError';
}
}
@@ -57,7 +59,8 @@ export class AuthenticationError extends CustomErrorBase {
*/
export class NotAllowedError extends CustomErrorBase {
constructor(message?: string, cause?: Error | unknown) {
super(message, cause, 'NotAllowedError');
super(message, cause);
this.name = 'NotAllowedError';
}
}
@@ -71,7 +74,8 @@ export class NotAllowedError extends CustomErrorBase {
*/
export class NotFoundError extends CustomErrorBase {
constructor(message?: string, cause?: Error | unknown) {
super(message, cause, 'NotFoundError');
super(message, cause);
this.name = 'NotFoundError';
}
}
@@ -83,7 +87,8 @@ export class NotFoundError extends CustomErrorBase {
*/
export class ConflictError extends CustomErrorBase {
constructor(message?: string, cause?: Error | unknown) {
super(message, cause, 'ConflictError');
super(message, cause);
this.name = 'ConflictError';
}
}
@@ -94,7 +99,8 @@ export class ConflictError extends CustomErrorBase {
*/
export class NotModifiedError extends CustomErrorBase {
constructor(message?: string, cause?: Error | unknown) {
super(message, cause, 'NotModifiedError');
super(message, cause);
this.name = 'NotModifiedError';
}
}
@@ -105,7 +111,8 @@ export class NotModifiedError extends CustomErrorBase {
*/
export class NotImplementedError extends CustomErrorBase {
constructor(message?: string, cause?: Error | unknown) {
super(message, cause, 'NotImplementedError');
super(message, cause);
this.name = 'NotImplementedError';
}
}
@@ -126,6 +133,8 @@ export class ServiceUnavailableError extends CustomErrorBase {}
*/
export class ForwardedError extends CustomErrorBase {
constructor(message: string, cause: Error | unknown) {
super(message, cause, isError(cause) ? cause.name : 'Error');
super(message, cause);
this.name = isError(cause) ? cause.name : 'Error';
}
}