errors: add explicit name property to ServiceUnavailableError

Every CustomErrorBase subclass explicitly sets `name = 'ClassName' as const`
except ServiceUnavailableError, which relied on the constructor fallback that
reads `this.constructor.name`. This is fragile under minification. Added the
explicit name property for consistency and added a serialization round-trip test.

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
Made-with: Cursor
This commit is contained in:
Patrik Oldsberg
2026-04-15 12:20:33 +02:00
parent a54350bc5b
commit 8741e5a800
4 changed files with 23 additions and 3 deletions
@@ -0,0 +1,5 @@
---
'@backstage/errors': patch
---
Added explicit `name` property to `ServiceUnavailableError` for consistency with all other error classes, making it resilient to minification.
+4 -1
View File
@@ -151,7 +151,10 @@ export function serializeError(
): SerializedError;
// @public
export class ServiceUnavailableError extends CustomErrorBase {}
export class ServiceUnavailableError extends CustomErrorBase {
// (undocumented)
name: 'ServiceUnavailableError';
}
// @public
export function stringifyError(error: unknown): string;
+3 -1
View File
@@ -100,7 +100,9 @@ export class NotImplementedError extends CustomErrorBase {
*
* @public
*/
export class ServiceUnavailableError extends CustomErrorBase {}
export class ServiceUnavailableError extends CustomErrorBase {
name = 'ServiceUnavailableError' as const;
}
/**
* An error that forwards an underlying cause with additional context in the message.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { NotModifiedError } from '../errors';
import { NotModifiedError, ServiceUnavailableError } from '../errors';
import { deserializeError, serializeError, stringifyError } from './error';
class CustomError extends Error {
@@ -77,6 +77,16 @@ describe('serialization', () => {
expect(withoutStack2.cause.stack).not.toBeDefined();
});
it('round-trips a ServiceUnavailableError', () => {
const before = new ServiceUnavailableError('service down');
const after = deserializeError(
serializeError(before, { includeStack: true }),
);
expect(after.name).toEqual('ServiceUnavailableError');
expect(after.message).toEqual('service down');
expect(after.stack).toEqual(before.stack);
});
it('stringifies all supported forms', () => {
expect(stringifyError({})).toEqual("unknown error '[object Object]'");
expect(