Metadata-Version: 2.3
Name: fastapi_exception
Version: 1.3.9
Summary: FastAPI Event
Project-URL: Homepage, https://github.com/megatron-global/fastapi-exception
Project-URL: Bug Tracker, https://github.com/megatron-global/fastapi-exception/issues
Author-email: Dzung Nguyen <dung@megatron-solutions.com>
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.11
Requires-Dist: fastapi-global-variable>=0.0.4
Requires-Dist: pydantic>=2.6.0
Requires-Dist: python-keycloak>=2.14.0
Description-Content-Type: text/markdown

# FastAPI Exception

## Installation

```shell
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

```python
# 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

```python
from fastapi_exception import throw_validation

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

### Use errors with throw_validation_with_exception

```python

from fastapi_exception import throw_validation_with_exception, MissingError

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

### Customize Exception

```python
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')))
```
