From e205b3e6ede8c64601fc87e26e778f064181c6c9 Mon Sep 17 00:00:00 2001 From: Mihailo Vlajkovic Date: Thu, 25 May 2023 17:44:24 +0200 Subject: [PATCH] Add name parameter to CustomErrorBase class Signed-off-by: Mihailo Vlajkovic --- .changeset/dirty-wasps-draw.md | 5 ++ packages/errors/src/errors/CustomErrorBase.ts | 5 +- packages/errors/src/errors/common.ts | 46 +++++++++++++++---- 3 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 .changeset/dirty-wasps-draw.md diff --git a/.changeset/dirty-wasps-draw.md b/.changeset/dirty-wasps-draw.md new file mode 100644 index 0000000000..89857259fb --- /dev/null +++ b/.changeset/dirty-wasps-draw.md @@ -0,0 +1,5 @@ +--- +'@backstage/errors': patch +--- + +Added a new parameter "name" to "CustomErrorBase" and updated all the existing extensions to use it diff --git a/packages/errors/src/errors/CustomErrorBase.ts b/packages/errors/src/errors/CustomErrorBase.ts index b831d4eea3..03bee4b4d9 100644 --- a/packages/errors/src/errors/CustomErrorBase.ts +++ b/packages/errors/src/errors/CustomErrorBase.ts @@ -19,6 +19,7 @@ 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 @@ -38,7 +39,7 @@ export class CustomErrorBase extends Error { */ readonly cause?: Error | undefined; - constructor(message?: string, cause?: Error | unknown) { + constructor(message?: string, cause?: Error | unknown, name?: string) { let fullMessage = message; if (cause !== undefined) { const causeStr = stringifyError(cause); @@ -53,7 +54,7 @@ export class CustomErrorBase extends Error { Error.captureStackTrace?.(this, this.constructor); - this.name = this.constructor.name; + this.name = name || 'CustomErrorBase'; this.cause = isError(cause) ? cause : undefined; } } diff --git a/packages/errors/src/errors/common.ts b/packages/errors/src/errors/common.ts index a2ea20e99f..e8663490f6 100644 --- a/packages/errors/src/errors/common.ts +++ b/packages/errors/src/errors/common.ts @@ -33,21 +33,33 @@ import { CustomErrorBase } from './CustomErrorBase'; * * @public */ -export class InputError extends CustomErrorBase {} +export class InputError extends CustomErrorBase { + constructor(message?: string, cause?: Error | unknown) { + super(message, cause, 'InputError'); + } +} /** * The request requires authentication, which was not properly supplied. * * @public */ -export class AuthenticationError extends CustomErrorBase {} +export class AuthenticationError extends CustomErrorBase { + constructor(message?: string, cause?: Error | unknown) { + super(message, cause, 'AuthenticationError'); + } +} /** * The authenticated caller is not allowed to perform this request. * * @public */ -export class NotAllowedError extends CustomErrorBase {} +export class NotAllowedError extends CustomErrorBase { + constructor(message?: string, cause?: Error | unknown) { + super(message, cause, 'NotAllowedError'); + } +} /** * The requested resource could not be found. @@ -57,7 +69,11 @@ export class NotAllowedError extends CustomErrorBase {} * * @public */ -export class NotFoundError extends CustomErrorBase {} +export class NotFoundError extends CustomErrorBase { + constructor(message?: string, cause?: Error | unknown) { + super(message, cause, 'NotFoundError'); + } +} /** * The request could not complete due to a conflict in the current state of the @@ -65,21 +81,33 @@ export class NotFoundError extends CustomErrorBase {} * * @public */ -export class ConflictError extends CustomErrorBase {} +export class ConflictError extends CustomErrorBase { + constructor(message?: string, cause?: Error | unknown) { + super(message, cause, 'ConflictError'); + } +} /** * The requested resource has not changed since last request. * * @public */ -export class NotModifiedError extends CustomErrorBase {} +export class NotModifiedError extends CustomErrorBase { + constructor(message?: string, cause?: Error | unknown) { + super(message, cause, 'NotModifiedError'); + } +} /** * The server does not support the functionality required to fulfill the request. * * @public */ -export class NotImplementedError extends CustomErrorBase {} +export class NotImplementedError extends CustomErrorBase { + constructor(message?: string, cause?: Error | unknown) { + super(message, cause, 'NotImplementedError'); + } +} /** * The server is not ready to handle the request. @@ -98,8 +126,6 @@ export class ServiceUnavailableError extends CustomErrorBase {} */ export class ForwardedError extends CustomErrorBase { constructor(message: string, cause: Error | unknown) { - super(message, cause); - - this.name = isError(cause) ? cause.name : 'Error'; + super(message, cause, isError(cause) ? cause.name : 'Error'); } }