-
Notifications
You must be signed in to change notification settings - Fork 171
feat(event-handler): add support for error handling in AppSync GraphQL #4317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dreamorosi
merged 46 commits into
aws-powertools:main
from
arnabrahman:4130-graphql-error-registry
Aug 27, 2025
Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
9f97957
feat: add types for exception handling in event handler
arnabrahman b588562
refactor: improve exception handling types for better type safety
arnabrahman c36c93c
feat: implement ExceptionHandlerRegistry for managing GraphQL excepti…
arnabrahman 150157e
feat: add exception handling support in AppSyncGraphQLResolver and Ro…
arnabrahman 432f5d6
test: add unit tests for ExceptionHandlerRegistry functionality
arnabrahman 081e805
feat: add exception handling for ValidationError, NotFoundError, and …
arnabrahman b7a2846
fix: correct import path for ExceptionHandlerRegistry in unit tests
arnabrahman d1a499d
fix: update import path for Router in unit tests
arnabrahman 2e1b853
fix: update exceptionHandler method signature for improved type handling
arnabrahman 3dc04b6
fix: update AssertionError usage in examples for consistency
arnabrahman 5df6159
feat: enhance exception handling by registering handlers for multiple…
arnabrahman 32919da
fix: update error message formatting in exception handler for clarity
arnabrahman d024aad
fix: update NotFoundError handling to use '0' as the identifier for n…
arnabrahman 66ff42e
fix: improve error handling in exception handler test for ValidationE…
arnabrahman aac7f9f
fix: update exception handler test to use synchronous handling for Va…
arnabrahman ad1793e
fix: update test descriptions to clarify exception handling behavior …
arnabrahman 737248d
fix: improve test descriptions for clarity in AppSyncGraphQLResolver
arnabrahman e93530c
fix: update error formatting to use constructor name in AppSyncGraphQ…
arnabrahman 737a766
fix: simplify error throwing logic in onQuery handler for better read…
arnabrahman b4d307c
fix: update error handling to use error name instead of constructor n…
arnabrahman 22433a6
doc: update documentation to clarify that exception handler resolutio…
arnabrahman eb049cf
doc: add missing region end comments for better code organization
arnabrahman 47c6c6a
test: add console error expectation for ValidationError in getUser ha…
arnabrahman 3a7d8dc
fix: update sync validation error message for clarity
arnabrahman 69ed880
feat: enhance error handling in AppSyncGraphQLResolver with NotFoundE…
arnabrahman e51c43b
fix: correct handler variable usage in ExceptionHandlerRegistry tests
arnabrahman e018758
doc: exception handling support in appsync-graphql
arnabrahman fb08824
fix: update exception handler logging to use error name for clarity
arnabrahman 433f10a
fix: improve error handling for AssertionError with detailed error st…
arnabrahman 4952c79
fix: update parameter name in exceptionHandler for clarity
arnabrahman 85b5c8c
fix: enhance exception handling documentation and response structure
arnabrahman 5aaac53
fix: clarify documentation for exception handler options and resolver…
arnabrahman c79f5bc
test: enhance test descriptions for clarity in exception handling sce…
arnabrahman 9366f86
feat: PR feedback resolved
arnabrahman 10adbb9
fix: enhance exception handling documentation and add example resolver
arnabrahman ddb217b
fix: revert Node.js version to 22.13.1 in Dockerfile
arnabrahman be3dc59
fix: sonarqube issue resolved
arnabrahman 8a02d3d
Update docs/features/event-handler/appsync-graphql.md
arnabrahman d7463c5
Update examples/snippets/event-handler/appsync-graphql/exceptionHandl…
arnabrahman 2600bde
Update examples/snippets/event-handler/appsync-graphql/exceptionHandl…
arnabrahman b2073ca
Update packages/event-handler/src/appsync-graphql/AppSyncGraphQLResol…
arnabrahman 7bb5882
Update packages/event-handler/tests/unit/appsync-graphql/AppSyncGraph…
arnabrahman 92e4952
Update packages/event-handler/tests/unit/appsync-graphql/AppSyncGraph…
arnabrahman 8b1b1e2
Update docs/features/event-handler/appsync-graphql.md
arnabrahman dd69bfb
fix: PR feedback handled
arnabrahman 4dcf918
Merge branch 'main' into 4130-graphql-error-registry
dreamorosi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
examples/snippets/event-handler/appsync-graphql/exceptionHandling.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
import { AssertionError } from 'node:assert'; | ||
import type { Context } from 'aws-lambda'; | ||
|
||
const logger = new Logger({ | ||
serviceName: 'MyService', | ||
}); | ||
const app = new AppSyncGraphQLResolver({ logger }); | ||
|
||
app.exceptionHandler(AssertionError, async (error) => { | ||
return { | ||
error: { | ||
message: error.message, | ||
type: error.name, | ||
}, | ||
}; | ||
}); | ||
|
||
app.onQuery('createSomething', async () => { | ||
throw new AssertionError({ | ||
message: 'This is an assertion error', | ||
}); | ||
}); | ||
|
||
export const handler = async (event: unknown, context: Context) => | ||
app.resolve(event, context); |
15 changes: 15 additions & 0 deletions
15
examples/snippets/event-handler/appsync-graphql/exceptionHandlingResolver.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { util } from '@aws-appsync/utils'; | ||
|
||
export function request(ctx) { | ||
return { | ||
operation: 'Invoke', | ||
payload: ctx, | ||
}; | ||
} | ||
|
||
export function response(ctx) { | ||
if (ctx.result.error) { | ||
return util.error(ctx.result.error.message, ctx.result.error.type); | ||
} | ||
return ctx.result; | ||
} |
23 changes: 23 additions & 0 deletions
23
examples/snippets/event-handler/appsync-graphql/exceptionHandlingResponse.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"data": { | ||
"createSomething": null | ||
}, | ||
"errors": [ | ||
{ | ||
"path": [ | ||
"createSomething" | ||
], | ||
"data": null, | ||
"errorType": "AssertionError", | ||
"errorInfo": null, | ||
"locations": [ | ||
{ | ||
"line": 2, | ||
"column": 3, | ||
"sourceName": null | ||
} | ||
], | ||
"message": "This is an assertion Error" | ||
} | ||
] | ||
} |
5 changes: 5 additions & 0 deletions
5
examples/snippets/event-handler/appsync-graphql/exceptionHandlingResponseMapping.vtl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#if (!$util.isNull($ctx.result.error)) | ||
$util.error($ctx.result.error.message, $ctx.result.error.type) | ||
#end | ||
|
||
$utils.toJson($ctx.result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/event-handler/src/appsync-graphql/ExceptionHandlerRegistry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { GenericLogger } from '@aws-lambda-powertools/commons/types'; | ||
import type { | ||
ErrorClass, | ||
ExceptionHandler, | ||
ExceptionHandlerOptions, | ||
ExceptionHandlerRegistryOptions, | ||
} from '../types/appsync-graphql.js'; | ||
|
||
/** | ||
* Registry for storing exception handlers for GraphQL resolvers in AWS AppSync GraphQL API's. | ||
*/ | ||
class ExceptionHandlerRegistry { | ||
/** | ||
* A map of registered exception handlers, keyed by their error class name. | ||
*/ | ||
protected readonly handlers: Map<string, ExceptionHandlerOptions> = new Map(); | ||
/** | ||
* A logger instance to be used for logging debug and warning messages. | ||
*/ | ||
readonly #logger: Pick<GenericLogger, 'debug' | 'warn' | 'error'>; | ||
|
||
public constructor(options: ExceptionHandlerRegistryOptions) { | ||
this.#logger = options.logger; | ||
} | ||
|
||
/** | ||
* Registers an exception handler for one or more error classes. | ||
* | ||
* If a handler for the given error class is already registered, it will be replaced and a warning will be logged. | ||
* | ||
* @param options - The options containing the error class(es) and their associated handler. | ||
* @param options.error - A single error class or an array of error classes to handle. | ||
* @param options.handler - The exception handler function that will be invoked when the error occurs. | ||
*/ | ||
public register(options: ExceptionHandlerOptions<Error>): void { | ||
const { error, handler } = options; | ||
const errors = Array.isArray(error) ? error : [error]; | ||
|
||
for (const err of errors) { | ||
this.registerErrorHandler(err, handler); | ||
} | ||
} | ||
|
||
/** | ||
* Registers a error handler for a specific error class. | ||
* | ||
* @param errorClass - The error class to register the handler for. | ||
* @param handler - The exception handler function. | ||
*/ | ||
private registerErrorHandler( | ||
errorClass: ErrorClass<Error>, | ||
handler: ExceptionHandler | ||
): void { | ||
const errorName = errorClass.name; | ||
|
||
this.#logger.debug(`Adding exception handler for error class ${errorName}`); | ||
|
||
if (this.handlers.has(errorName)) { | ||
this.#logger.warn( | ||
`An exception handler for error class '${errorName}' is already registered. The previous handler will be replaced.` | ||
); | ||
} | ||
|
||
this.handlers.set(errorName, { | ||
error: errorClass, | ||
handler, | ||
}); | ||
} | ||
|
||
/** | ||
* Resolves and returns the appropriate exception handler for a given error instance. | ||
* | ||
* This method attempts to find a registered exception handler based on the error class name. | ||
* If a matching handler is found, it is returned; otherwise, `null` is returned. | ||
* | ||
* @param error - The error instance for which to resolve an exception handler. | ||
*/ | ||
public resolve(error: Error): ExceptionHandler | null { | ||
const errorName = error.name; | ||
this.#logger.debug(`Looking for exception handler for error: ${errorName}`); | ||
|
||
const handlerOptions = this.handlers.get(errorName); | ||
if (handlerOptions) { | ||
this.#logger.debug(`Found exact match for error class: ${errorName}`); | ||
return handlerOptions.handler; | ||
} | ||
|
||
this.#logger.debug(`No exception handler found for error: ${errorName}`); | ||
return null; | ||
} | ||
} | ||
|
||
export { ExceptionHandlerRegistry }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.