Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Oct 18, 2024
1 parent bf1bfae commit 2da3161
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs_src/src/pages/documentation/api_reference/exceptions.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
## Handling Errors

Batman learned how to handle the error for different exception in his application. He wrote the following code to handle exceptions :

<Row>
<Col>
</Col>
<Col sticky>

<CodeGroup title="Request" tag="GET" label="/hello_world/{item_id}">

```python {{ title: 'untyped' }}
from robyn import Robyn, HTTPException, status_codes

app = Robyn(__file__)

items = {"foo": "The Foo Wrestlers"}

@app.get("/hello_world/{item_id}")
async def read_item(request, item_id):
if item_id not in items:
raise HTTPException(status_code=status_codes.HTTP_404_NOT_FOUND, detail="Item not found")
return {"item": items[item_id]}
```

```python {{ title: 'typed' }}
from robyn import Robyn, HTTPException, Request, status_codes
from typing import Dict

app = Robyn(__file__)

items: Dict[str, str] = {"foo": "The Foo Wrestlers"}


@app.get("/hello_world/{item_id}")
async def read_item(request: Request, item_id: str) -> Dict[str, str]:
if item_id not in items:
raise HTTPException(status_code=status_codes.HTTP_404_NOT_FOUND, detail="Item not found")
return {"item": items[item_id]}

```
</CodeGroup>
</Col>
</Row>

## Custom Exception Handler

Batman learned how to create custom error handlers for different exception types in his application. He wrote the following code to handle exceptions and return a custom error response:
Expand Down

0 comments on commit 2da3161

Please sign in to comment.