Skip to main content

FastAPI Event

Project description

FastAPI Exception

Installation

pip install fastapi-exception

How to use

fastapi_exception exposes exceptions

  • FastApiException
  • ForbiddenException
  • NotFoundException
  • GoneException
  • BadRequestException
  • DirectResponseException
  • EntityNotFoundException
  • UnauthorizedException

fastapi_exception exposes errors

  • DuplicateError
  • StringTooLongError
  • StringTooShortError
  • MissingError

Configuration

# config/exception.py
from config.i18n import i18n_service
from fastapi_exception import FastApiException

FastApiException.init()
FastApiException.init(translator_service=i18n_service) # pass translator_service if we integrate with i18n

Use exceptions with throw_validation

from fastapi_exception import throw_validation

throw_validation(type='value_error', loc=('body', 'key'))

Use errors with throw_validation_with_exception

from fastapi_exception import throw_validation_with_exception, MissingError

raise throw_validation_with_exception(MissingError(('body', 'product media or galleries')))

Customize Exception

from fastapi_exception import ValidationErrorDetail

class InvalidDataTypeError(ValidationErrorDetail):
  error_type = 'datatype.invalid'

  def __init__(
    self,
    loc: tuple[int | str, ...],
    ctx: dict[str, dict[str, Any]] | None = {},
    input: dict[str, Any] = {},
  ):
    super().__init__(self.error_type, loc, '', input, ctx)

throw_validation_with_exception(InvalidDataTypeError(('body', 'value')))

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

fastapi_exception-1.3.9.tar.gz (11.3 kB view hashes)

Uploaded Source

Built Distribution

fastapi_exception-1.3.9-py3-none-any.whl (13.9 kB view hashes)

Uploaded Python 3

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