Skip to content

Commit e018758

Browse files
committed
doc: exception handling support in appsync-graphql
1 parent e51c43b commit e018758

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

docs/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
FROM squidfunk/mkdocs-material@sha256:bb7b015690d9fb5ef0dbc98ca3520f153aa43129fb96aec5ca54c9154dc3b729
33

44
# Install Node.js
5-
RUN apk add --no-cache nodejs=22.13.1-r0 npm
5+
RUN apk add --no-cache nodejs=22.15.1-r0 npm
66

77
COPY requirements.txt /tmp/
88
RUN pip install --require-hashes -r /tmp/requirements.txt

docs/features/event-handler/appsync-graphql.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,30 @@ You can access the original Lambda event or context for additional information.
148148

149149
1. The `event` parameter contains the original AppSync event and has type `AppSyncResolverEvent` from the `@types/aws-lambda`.
150150

151+
### Exception Handling
152+
153+
You can use the **`exceptionHandler`** method with any exception. This allows you to handle a common exception outside your resolver.
154+
155+
When using exception handlers, you'll also need to configure your AppSync response mapping template to properly handle the custom error responses.
156+
157+
=== "Exception Handling"
158+
159+
```typescript hl_lines="11-13 16-18"
160+
--8<-- "examples/snippets/event-handler/appsync-graphql/exceptionHandling.ts"
161+
```
162+
163+
=== "VTL Response Mapping Template"
164+
165+
```velocity hl_lines="1-3"
166+
--8<-- "examples/snippets/event-handler/appsync-graphql/exceptionHandlingResponseMapping.vtl"
167+
```
168+
169+
=== "Exception Handling response"
170+
171+
```json hl_lines="11 20"
172+
--8<-- "examples/snippets/event-handler/appsync-graphql/exceptionHandlingResponse.json"
173+
```
174+
151175
### Logging
152176

153177
By default, the utility uses the global `console` logger and emits only warnings and errors.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
2+
import { Logger } from '@aws-lambda-powertools/logger';
3+
import { AssertionError } from 'assert';
4+
import type { Context } from 'aws-lambda';
5+
6+
const logger = new Logger({
7+
serviceName: 'MyService',
8+
});
9+
const app = new AppSyncGraphQLResolver({ logger });
10+
11+
app.exceptionHandler(AssertionError, async (error) => {
12+
return { error: { message: error.message, type: error.name } };
13+
});
14+
15+
app.onQuery('createSomething', async () => {
16+
throw new AssertionError({
17+
message: 'This is an assertion Error',
18+
});
19+
});
20+
21+
export const handler = async (event: unknown, context: Context) =>
22+
app.resolve(event, context);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"data": {
3+
"createSomething": null
4+
},
5+
"errors": [
6+
{
7+
"path": [
8+
"createSomething"
9+
],
10+
"data": null,
11+
"errorType": "AssertionError",
12+
"errorInfo": null,
13+
"locations": [
14+
{
15+
"line": 2,
16+
"column": 3,
17+
"sourceName": null
18+
}
19+
],
20+
"message": "This is an assertion Error"
21+
}
22+
]
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#if (!$util.isNull($ctx.result.error))
2+
$util.error($ctx.result.error.message, $ctx.result.error.type)
3+
#end
4+
5+
$utils.toJson($ctx.result)

0 commit comments

Comments
 (0)