Skip to main content

Web based error utils

Project description

Web Errors v0.5.2

image image image style tests codecov

web_error is a set of exceptions and handlers for use in web apis to support easy error management and responses

Each exception easily marshals to JSON for use in api errors. Handlers for different web frameworks are provided.

Errors

The base web_error.error.HttpException accepts a message, debug_message, code and status (default 500)

And will render a response with status as the status code:

{
    "code": "code",
    "message": "message",
    "debug_message": "debug_message",
}

Some convenience Exceptions are provided, to create custom error subclass these and define message and code attributes.

  • web_error.error.ServerException provides status 500 errors
  • web_error.error.BadRequestException provides status 400 errors
  • web_error.error.UnauthorisedException provides status 401 errors
  • web_error.error.NotFoundException provides status 404 errors

Custom Errors

Subclassing the convenience classes provide a simple way to consistently raise the same error and message.

Code is an optional attribute to provide a unique value to parse in a frontend/client instead of matching against messages.

from web_error.error import NotFoundException


class UserNotFoundError(NotFoundException):
    message = "User not found."
    code = "E001"

FastAPI

Include the exception_handler in your app.

    exception_handler = web_error.handler.fastapi.exception_handler

    return FastAPI(
        exception_handlers={
            Exception: exception_handler,
            RequestValidationError: exception_handler,
            HTTPException: exception_handler,
        },
    )

Pyramid

Include the pyramid exception handlers in your config.

def main(global_config, **config_blob):
    config = Configurator(settings=config_blob)

    ...

    config.scan("web_error.handler.pyramid")

    return config.make_wsgi_app()

This will handle all unexpected errors, and any app specific errors.

@view_config(route_name="test", renderer="json")
def test(request):
    raise UserNotFoundError("debug message")

Flask

Register the error handler with your application

app.register_error_handler(Exception, web_error.handler.flask.exception_handler)

Aiohttp

Decorate your views with the error handler.

@web_error.handler.aiohttp.view_error_handler
async def user(self):
    raise UserNotFoundError("debug message")

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

web_error-0.5.2.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

web_error-0.5.2-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file web_error-0.5.2.tar.gz.

File metadata

  • Download URL: web_error-0.5.2.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.9 Linux/6.1.7-zen1-1-zen

File hashes

Hashes for web_error-0.5.2.tar.gz
Algorithm Hash digest
SHA256 2b0963fe3411159fd40a44f98f7371adf7595b86e40fb9efc4df7da19ee2a402
MD5 085fb2d651d6590101ed1b90bee4c2ec
BLAKE2b-256 3ebf9aca369f8ea19b1000c59d06bc3c69ec8d4f7ff7512e150766f5271c2cf7

See more details on using hashes here.

Provenance

File details

Details for the file web_error-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: web_error-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.9 Linux/6.1.7-zen1-1-zen

File hashes

Hashes for web_error-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9603c2e93235d0ef3f18fa1f2d358bb4cc17023cd28d3a1085ead17d3a48f5c0
MD5 782a9a26e93d48f000a5e181cb6e1f77
BLAKE2b-256 dae68f0627cd8187c68487111ba0d9d75782d5c9f613d046d3d2de37d4d2f659

See more details on using hashes here.

Provenance

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