Skip to main content

Beautiful debugging page for Starlette apps.

Project description

Starception

Beautiful exception page for Starlette and FastAPI apps.

PyPI GitHub Workflow Status GitHub Libraries.io dependency status for latest release PyPI - Downloads GitHub Release Date

Installation

Install starception using PIP or poetry:

pip install starception
# or
poetry add starception

Screenshot

image

Features

  • secrets masking
  • solution hints
  • code snippets
  • display request info: query, body, headers, cookies
  • session contents
  • request and app state
  • platform information
  • environment variables
  • syntax highlight
  • open paths in editor (vscode only)
  • exception chains
  • dark theme

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

Quick start

See example application in 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

To replace built-in debug exception handler call install_error_handler before you create Starlette instance.

Currently, this is a recommended approach.

from starception import install_error_handler
from starlette.applications import Starlette

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.

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:

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 framework. But keep in mind, Starlette will not call any custom exception handler in debug mode (it always uses built-in one).

The snipped below will not work as you expect (unfortunately).

from starlette.applications import Starlette

from starception import exception_handler

app = Starlette(
    debug=True,
    exception_handlers={Exception: exception_handler}
)

Solution hints

If exception class has solution attribute then its content will be used as a solution hint.

class WithHintError(Exception):
    solution = (
        'The connection to the database cannot be established. '
        'Either the database server is down or connection credentials are invalid.'
    )

image

Opening files in editor

Set your current editor to open paths in your editor/IDE.

from starception import set_editor

set_editor('vscode')

image

Note, currently only VSCode supported. If you know how to integrate other editors - please PR

Registering link templates

If your editor is not supported, you can add it by calling add_link_template and then selecting it with set_editor.

from starception import set_editor, add_link_template

add_link_template('vscode', 'vscode://file/{path}:{lineno}')
set_editor('vscode')

Dark theme

Switch between light/dark themes using set_theme utility.

Currently, we use pygments for syntax highlighting, therefore theme must be set in the code. In future releases we may highlight code on client side, and then we can make Starception to follow your browser's theme.

from starception import set_theme

set_theme('dark')

image

Credentials

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starception-0.5.1.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

starception-0.5.1-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file starception-0.5.1.tar.gz.

File metadata

  • Download URL: starception-0.5.1.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.7 Linux/5.15.0-1020-azure

File hashes

Hashes for starception-0.5.1.tar.gz
Algorithm Hash digest
SHA256 9de228914bb5a422851bbbf5d702347d7ec5cf9b1f4e12673474ece55be707be
MD5 a4da754d3888e91b78a74e2e30a4c915
BLAKE2b-256 01c68d0029d74a57e4c0dd5396e5461827ba5e6b24cc91853a9420b778e1e74b

See more details on using hashes here.

File details

Details for the file starception-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: starception-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.7 Linux/5.15.0-1020-azure

File hashes

Hashes for starception-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da8f33386fc776e02339a2ff7579dbdc6ffea215b839178b35e30da08d303b7b
MD5 aa3fe3211dbd53528fab4aeaed9e517c
BLAKE2b-256 f6634f4dec09472cf04ba0fca2b47020ce92c34accfcf444cba2b4676e5b2e58

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page