Skip to main content

A library for handling HTTP exceptions in Starlette applications, providing a set of predefined exceptions for common HTTP error statuses like 404, 403, 401, etc., to make error handling in APIs more streamlined and maintainable.

Project description

Custom HTTP Exceptions for Starlette

Project Description

This library provides a set of custom HTTP exceptions for Starlette applications, inspired by Starlette's built-in exceptions. With these custom exceptions, you can simplify error handling by using pre-configured, standard HTTP exception classes tailored to common HTTP status codes.

Installation

To get started, simply install the package via pip:

pip install starlette-http-exceptions

Usage

Instead of duplicating exception handling code throughout your Starlette application, you can use the pre-defined exceptions from this library. Raise these exceptions directly without worrying about writing boilerplate code for every status code.

Example 1: Raising Unauthorized Exception

Instead of manually creating HTTP exceptions like this:

from fastapi import HTTPException, status

# Some application logic...
raise HTTPException(
    status_code=status.HTTP_401_UNAUTHORIZED,
    detail="Authentication credentials were not provided"
)

You can import and use a pre-configured exception like this:

from starlette_http_exceptions import UnauthorizedException

# Some application logic...
raise UnauthorizedException(
  detail="Authentication credentials were not provided",
  headers={}
)

Available Exceptions

Exception Status Code
BadRequestException 400 Bad Request
UnauthorizedException 401 Unauthorized
ForbiddenException 403 Forbidden
NotFoundException 404 Not Found
MethodNotAllowedException 405 Method Not Allowed
NotAcceptableException 406 Not Acceptable
ProxyAuthenticationRequiredException 407 Proxy Authentication Required
RequestTimeoutException 408 Request Timeout
ConflictException 409 Conflict
GoneException 410 Gone
LengthRequiredException 411 Length Required
PreconditionFailedException 412 Precondition Failed
RequestEntityTooLargeException 413 Request Entity Too Large
RequestUriTooLongException 414 Request URI Too Long
UnsupportedMediaTypeException 415 Unsupported Media Type
RequestedRangeNotSatisfiableException 416 Requested Range Not Satisfiable
ExpectationFailedException 417 Expectation Failed
ImATeapotException 418 I'm a Teapot
MisdirectedRequestException 421 Misdirected Request
UnprocessableEntityException 422 Unprocessable Entity
LockedException 423 Locked
FailedDependencyException 424 Failed Dependency
UpgradeRequiredException 426 Upgrade Required
PreconditionRequiredException 428 Precondition Required
TooManyRequestsException 429 Too Many Requests
RequestHeaderFieldsTooLargeException 431 Request Header Fields Too Large
UnavailableForLegalReasonsException 451 Unavailable For Legal Reasons

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

starlette_http_exceptions-0.1.2.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_http_exceptions-0.1.2-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file starlette_http_exceptions-0.1.2.tar.gz.

File metadata

File hashes

Hashes for starlette_http_exceptions-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5185204e379650b8b3abe020f667e8ded80b4163b05de6c59d686a01f3e8e679
MD5 b277e1a5ed818bf016c3185c4ddd6420
BLAKE2b-256 e7d76b34d59e817edaa42991d477655653274d7201a3091c5f23e95645c7d501

See more details on using hashes here.

File details

Details for the file starlette_http_exceptions-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_http_exceptions-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 14b9d65219368441d3773e8c7dd1d02f8603bb210b6ec92790f8ade068caa230
MD5 f249462382bbeb802b776a902b67c3c0
BLAKE2b-256 b2b3e24d36085c5b8ef3d6dd7be9ab250061ae2f07464d596a5a14dda79d490c

See more details on using hashes here.

Supported by

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