Skip to content

Commit 7efcf9d

Browse files
Adding exception handler support - fix tests + docs
1 parent 11bd6f9 commit 7efcf9d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

docs/core/event_handler/appsync.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,19 @@ You can use `append_context` when you want to share data between your App and Ro
288288
--8<-- "examples/event_handler_graphql/src/split_operation_append_context_module.py"
289289
```
290290

291+
### Exception handling
292+
293+
You can use **`exception_handler`** decorator with any Python exception. This allows you to handle a common exception outside your resolver, for example validation errors.
294+
295+
The `exception_handler` function also supports passing a list of exception types you wish to handle with one handler.
296+
297+
```python hl_lines="5-7 11" title="Exception handling"
298+
--8<-- "examples/event_handler_graphql/src/exception_handling_graphql.py"
299+
```
300+
301+
???+ warning
302+
This is not supported when using async single resolvers.
303+
291304
### Batch processing
292305

293306
```mermaid
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from aws_lambda_powertools.event_handler import AppSyncResolver
2+
3+
app = AppSyncResolver()
4+
5+
6+
@app.exception_handler(ValueError)
7+
def handle_value_error(ex: ValueError):
8+
return {"message": "error"}
9+
10+
11+
@app.resolver(field_name="createSomething")
12+
def create_something():
13+
raise ValueError("Raising an exception")
14+
15+
16+
def lambda_handler(event, context):
17+
return app.resolve(event, context)

0 commit comments

Comments
 (0)