Skip to content

Commit b4d307c

Browse files
committed
fix: update error handling to use error name instead of constructor name for better clarity
1 parent 737a766 commit b4d307c

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

packages/event-handler/src/appsync-graphql/AppSyncGraphQLResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ class AppSyncGraphQLResolver extends Router {
398398
#formatErrorResponse(error: unknown) {
399399
if (error instanceof Error) {
400400
return {
401-
error: `${error.constructor.name} - ${error.message}`,
401+
error: `${error.name} - ${error.message}`,
402402
};
403403
}
404404
return {

packages/event-handler/src/appsync-graphql/ExceptionHandlerRegistry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ExceptionHandlerRegistry {
5656
* @param error - The error instance for which to resolve an exception handler.
5757
*/
5858
public resolve(error: Error): ExceptionHandler | undefined {
59-
const errorName = error.constructor.name;
59+
const errorName = error.name;
6060
this.#logger.debug(`Looking for exception handler for error: ${errorName}`);
6161

6262
const handlerOptions = this.handlers.get(errorName);

packages/event-handler/tests/unit/appsync-graphql/AppSyncGraphQLResolver.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,24 @@ import {
1010
import type { ErrorClass } from '../../../src/types/appsync-graphql.js';
1111
import { onGraphqlEventFactory } from '../../helpers/factories.js';
1212

13-
class ValidationError extends Error {}
14-
class NotFoundError extends Error {}
15-
class DatabaseError extends Error {}
13+
class ValidationError extends Error {
14+
constructor(message: string, options?: ErrorOptions) {
15+
super(message, options);
16+
this.name = 'ValidationError';
17+
}
18+
}
19+
class NotFoundError extends Error {
20+
constructor(message: string, options?: ErrorOptions) {
21+
super(message, options);
22+
this.name = 'NotFoundError';
23+
}
24+
}
25+
class DatabaseError extends Error {
26+
constructor(message: string, options?: ErrorOptions) {
27+
super(message, options);
28+
this.name = 'DatabaseError';
29+
}
30+
}
1631

1732
describe('Class: AppSyncGraphQLResolver', () => {
1833
beforeEach(() => {

packages/event-handler/tests/unit/appsync-graphql/ExceptionHandlerRegistry.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ describe('Class: ExceptionHandlerRegistry', () => {
66
class MockExceptionHandlerRegistry extends ExceptionHandlerRegistry {
77
public declare handlers: Map<string, ExceptionHandlerOptions>;
88
}
9-
class CustomError extends Error {}
9+
class CustomError extends Error {
10+
constructor(message: string) {
11+
super(message);
12+
this.name = 'CustomError';
13+
}
14+
}
1015

1116
const getRegistry = () =>
1217
new MockExceptionHandlerRegistry({ logger: console });

0 commit comments

Comments
 (0)