diff --git a/.changeset/dirty-wasps-draw.md b/.changeset/dirty-wasps-draw.md new file mode 100644 index 0000000000..5686191cab --- /dev/null +++ b/.changeset/dirty-wasps-draw.md @@ -0,0 +1,5 @@ +--- +'@backstage/errors': patch +--- + +Set `this.name` in all error classes that extend `CustomErrorBase` class to their actual name diff --git a/packages/errors/api-report.md b/packages/errors/api-report.md index 0d405d8ba6..3ece63035b 100644 --- a/packages/errors/api-report.md +++ b/packages/errors/api-report.md @@ -9,10 +9,16 @@ import { JsonObject } from '@backstage/types'; export function assertError(value: unknown): asserts value is ErrorLike; // @public -export class AuthenticationError extends CustomErrorBase {} +export class AuthenticationError extends CustomErrorBase { + // (undocumented) + name: 'AuthenticationError'; +} // @public -export class ConflictError extends CustomErrorBase {} +export class ConflictError extends CustomErrorBase { + // (undocumented) + name: 'ConflictError'; +} // @public export type ConsumedResponse = { @@ -73,22 +79,37 @@ export class ForwardedError extends CustomErrorBase { } // @public -export class InputError extends CustomErrorBase {} +export class InputError extends CustomErrorBase { + // (undocumented) + name: 'InputError'; +} // @public export function isError(value: unknown): value is ErrorLike; // @public -export class NotAllowedError extends CustomErrorBase {} +export class NotAllowedError extends CustomErrorBase { + // (undocumented) + name: 'NotAllowedError'; +} // @public -export class NotFoundError extends CustomErrorBase {} +export class NotFoundError extends CustomErrorBase { + // (undocumented) + name: 'NotFoundError'; +} // @public -export class NotImplementedError extends CustomErrorBase {} +export class NotImplementedError extends CustomErrorBase { + // (undocumented) + name: 'NotImplementedError'; +} // @public -export class NotModifiedError extends CustomErrorBase {} +export class NotModifiedError extends CustomErrorBase { + // (undocumented) + name: 'NotModifiedError'; +} // @public export function parseErrorResponseBody( diff --git a/packages/errors/src/errors/CustomErrorBase.ts b/packages/errors/src/errors/CustomErrorBase.ts index b831d4eea3..ea9c91f173 100644 --- a/packages/errors/src/errors/CustomErrorBase.ts +++ b/packages/errors/src/errors/CustomErrorBase.ts @@ -22,8 +22,11 @@ import { isError } from './assertion'; * * @public * @example - *```ts - * class MyCustomError extends CustomErrorBase {} + * + * ```ts + * class MyCustomError extends CustomErrorBase { + * name = 'MyCustomError' as const; + * } * * const e = new MyCustomError('Some message', cause); * // e.name === 'MyCustomError' @@ -53,7 +56,13 @@ export class CustomErrorBase extends Error { Error.captureStackTrace?.(this, this.constructor); - this.name = this.constructor.name; + if (!this.name || this.name === 'Error') { + const baseName = this.constructor.name; + if (baseName !== 'Error') { + this.name = this.constructor.name; + } + } + this.cause = isError(cause) ? cause : undefined; } } diff --git a/packages/errors/src/errors/common.ts b/packages/errors/src/errors/common.ts index a2ea20e99f..861c3a7c3a 100644 --- a/packages/errors/src/errors/common.ts +++ b/packages/errors/src/errors/common.ts @@ -33,21 +33,27 @@ import { CustomErrorBase } from './CustomErrorBase'; * * @public */ -export class InputError extends CustomErrorBase {} +export class InputError extends CustomErrorBase { + name = 'InputError' as const; +} /** * The request requires authentication, which was not properly supplied. * * @public */ -export class AuthenticationError extends CustomErrorBase {} +export class AuthenticationError extends CustomErrorBase { + name = 'AuthenticationError' as const; +} /** * The authenticated caller is not allowed to perform this request. * * @public */ -export class NotAllowedError extends CustomErrorBase {} +export class NotAllowedError extends CustomErrorBase { + name = 'NotAllowedError' as const; +} /** * The requested resource could not be found. @@ -57,7 +63,9 @@ export class NotAllowedError extends CustomErrorBase {} * * @public */ -export class NotFoundError extends CustomErrorBase {} +export class NotFoundError extends CustomErrorBase { + name = 'NotFoundError' as const; +} /** * The request could not complete due to a conflict in the current state of the @@ -65,21 +73,27 @@ export class NotFoundError extends CustomErrorBase {} * * @public */ -export class ConflictError extends CustomErrorBase {} +export class ConflictError extends CustomErrorBase { + name = 'ConflictError' as const; +} /** * The requested resource has not changed since last request. * * @public */ -export class NotModifiedError extends CustomErrorBase {} +export class NotModifiedError extends CustomErrorBase { + name = 'NotModifiedError' as const; +} /** * The server does not support the functionality required to fulfill the request. * * @public */ -export class NotImplementedError extends CustomErrorBase {} +export class NotImplementedError extends CustomErrorBase { + name = 'NotImplementedError' as const; +} /** * The server is not ready to handle the request.