Skip to content

Commit

Permalink
Merge pull request #33 from alex-oleshkevich/remove-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-oleshkevich committed Jun 21, 2023
2 parents b68b6ea + 6e1a351 commit 851d312
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 157 deletions.
59 changes: 2 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,17 @@ poetry add starception -E pygments
* exception chains
* dark theme

The middleware will automatically mask any value which key contains `key`, `secret`, `token`, `password`.
Starception automatically masks any value which key contains `key`, `secret`, `token`, `password`.

## Quick start

See example application in [examples/](examples/) directory of this repository.

## Usage

Starception will work only in debug mode so don't forget to set `debug=True` for local development.

### Monkey patching Starlette
Starception will work only in debug mode so don't forget to set `Starlette.debug=True`.

To replace built-in debug exception handler call `install_error_handler` before you create Starlette instance.
> Currently, this is a recommended approach.

```python
from starception import install_error_handler
Expand All @@ -78,58 +75,6 @@ install_error_handler()
app = Starlette()
```

### Using middleware

To render a beautiful exception page you need to install a `StarceptionMiddleware` middleware to your application.


> Note, to catch as many exceptions as possible the middleware has to be the first one in the stack.
```python
import typing

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.routing import Route

from starception import StarceptionMiddleware


async def index_view(request: Request) -> typing.NoReturn:
raise TypeError('Oops, something really went wrong...')


app = Starlette(
debug=True,
routes=[Route('/', index_view)],
middleware=[
Middleware(StarceptionMiddleware),
# other middleware go here
],
)
```

### Integration with FastAPI

Attach `StarceptionMiddleware` middleware to your FastAPI application:

```python
import typing

from fastapi import FastAPI, Request

from starception import StarceptionMiddleware

app = FastAPI(debug=True)
app.add_middleware(StarceptionMiddleware) # must be the first one!


@app.route('/')
async def index_view(request: Request) -> typing.NoReturn:
raise TypeError('Oops, something really went wrong...')
```

### Integration with other frameworks

`starception` exports `starception.exception_handler(request, exc)` function, which you can use in your
Expand Down
14 changes: 0 additions & 14 deletions examples/fastapi.py

This file was deleted.

2 changes: 0 additions & 2 deletions starception/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from starception.exception_handler import add_link_template, exception_handler, install_error_handler, set_editor
from starception.middleware import StarceptionMiddleware

__all__ = [
'StarceptionMiddleware',
'exception_handler',
'set_editor',
'add_link_template',
Expand Down
60 changes: 0 additions & 60 deletions starception/middleware.py

This file was deleted.

24 changes: 0 additions & 24 deletions tests/test_middleware.py → tests/test_starception.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
import typing
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.routing import Route
from starlette.testclient import TestClient

from starception import StarceptionMiddleware
from starception.exception_handler import install_error_handler


def view(request: Request) -> typing.NoReturn:
raise TypeError('Oops')


debug_app = Starlette(debug=True, routes=[Route('/', view)], middleware=[Middleware(StarceptionMiddleware)])
release_app = Starlette(debug=False, routes=[Route('/', view)], middleware=[Middleware(StarceptionMiddleware)])
no_middleware_debug_app = Starlette(debug=True, routes=[Route('/', view)])
no_middleware_release_app = Starlette(debug=False, routes=[Route('/', view)])


def test_middleware_renders_html_page_in_debug_mode() -> None:
client = TestClient(debug_app, raise_server_exceptions=False)
response = client.get('/', headers={'accept': 'text/html'})
assert '<body>' in response.text
assert 'Oops' in response.text


def test_middleware_renders_html_page_with_handler_installed_in_debug_mode() -> None:
install_error_handler()
client = TestClient(no_middleware_debug_app, raise_server_exceptions=False)
Expand All @@ -39,16 +28,3 @@ def test_middleware_renders_plain_text_page_with_handler_installed_in_release_mo
client = TestClient(no_middleware_release_app, raise_server_exceptions=False)
response = client.get('/', headers={'accept': 'text/html'})
assert 'Internal Server Error' in response.text


def test_middleware_renders_plain_text_page_in_debug_mode_for_non_html() -> None:
client = TestClient(debug_app, raise_server_exceptions=False)
response = client.get('/', headers={'accept': 'text/plain'})
assert '<body>' not in response.text
assert 'Oops' in response.text


def test_middleware_renders_plain_text_in_release_mode() -> None:
client = TestClient(release_app, raise_server_exceptions=False)
response = client.get('/', headers={'accept': 'text/html'})
assert 'Internal Server Error' in response.text

0 comments on commit 851d312

Please sign in to comment.