Skip to content

Commit

Permalink
Add API error handling usage example.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroCabeza committed Nov 13, 2021
1 parent 84e2caa commit 10e5a94
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,38 @@ There's a priority hierarchy to know what handler to use:
# Installation

`pip install flexceptions`

# Usage Examples

### API Error Handling
One use case I like abstracting with this library is an exception hierarchy system that is castable to an HTTP Response object. Then, by decorating views with `handle_flexception`, every raised Flexception will have its `handle` method called, ensuring a proper object is sent back.

It's also worth to mention that the `handle_flexception` decorator accepts a `handler` that is passed on to the `handle` method.
```python
from typing import Dict
from flexceptions import BaseFlexception, Handler, handle_flexception


class Request:
...

class Response:
...


def _transform_to_http_response(exception: "MyApiException") -> Response:
return Response(status_code=exception.status_code, json=exception.json)


class MyApiException(BaseFlexception):
DEFAULT_HANDLER: Handler = _transform_to_http_response

def __init__(self, status_code: int = 400, json: Dict = None) -> None:
self.status_code: int = status_code
self.json: Dict = json or dict()


@handle_flexception
def view(request: Request) -> Response:
...
```

0 comments on commit 10e5a94

Please sign in to comment.