Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ message:
"EXCEPTION_HANDLER_CLASS": "path.to.MyExceptionHandler"
}
```
- set minimum version of drf-spectacular to 0.27.1
- `drf_standardized_errors.types.ErrorType` is now the following type hint
```python
from typing import Literal
ErrorType = Literal["validation_error", "client_error", "server_error"]
```
`ErrorType` was previously an enum. If you referenced its members in your code, make sure to replace their
use cases with the newly added constants:
```
from drf_standardized_errors.types import VALIDATION_ERROR, CLIENT_ERROR, SERVER_ERROR
ErrorType.VALIDATION_ERROR --> VALIDATION_ERROR
ErrorType.CLIENT_ERROR --> CLIENT_ERROR
ErrorType.SERVER_ERROR --> SERVER_ERROR
```

## [0.14.1] - 2024-08-10
### Added
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ REST_FRAMEWORK = {

### Already using a custom `AutoSchema` class
If you're already overriding the `AutoSchema` class provided by drf-spectacular, be sure to inherit from the
AutoSchema class provided by this package instead. Also, if you're overriding `get_examples` and/or
AutoSchema class provided by this package instead. Also, if you're overriding `_get_examples` and/or
`_get_response_bodies`, be sure to call `super`.


Expand Down
16 changes: 12 additions & 4 deletions drf_standardized_errors/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
from rest_framework.status import is_client_error

from .settings import package_settings
from .types import Error, ErrorResponse, ErrorType, ExceptionHandlerContext
from .types import (
CLIENT_ERROR,
SERVER_ERROR,
VALIDATION_ERROR,
Error,
ErrorResponse,
ErrorType,
ExceptionHandlerContext,
)


class ExceptionFormatter:
Expand Down Expand Up @@ -42,11 +50,11 @@ def run(self) -> Any:

def get_error_type(self) -> ErrorType:
if isinstance(self.exc, exceptions.ValidationError):
return ErrorType.VALIDATION_ERROR
return VALIDATION_ERROR
elif is_client_error(self.exc.status_code):
return ErrorType.CLIENT_ERROR
return CLIENT_ERROR
else:
return ErrorType.SERVER_ERROR
return SERVER_ERROR

def get_errors(self) -> List[Error]:
"""
Expand Down
11 changes: 5 additions & 6 deletions drf_standardized_errors/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from dataclasses import dataclass
from enum import Enum
from typing import List, Optional, TypedDict
from typing import Final, List, Literal, Optional, TypedDict

from rest_framework.request import Request
from rest_framework.views import APIView
Expand All @@ -13,10 +12,10 @@ class ExceptionHandlerContext(TypedDict):
request: Optional[Request]


class ErrorType(str, Enum):
VALIDATION_ERROR = "validation_error"
CLIENT_ERROR = "client_error"
SERVER_ERROR = "server_error"
VALIDATION_ERROR: Final = "validation_error"
CLIENT_ERROR: Final = "client_error"
SERVER_ERROR: Final = "server_error"
ErrorType = Literal["validation_error", "client_error", "server_error"]


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ release = [
"tbump",
]
openapi = [
"drf-spectacular>=0.27.0",
"drf-spectacular>=0.27.1",
"inflection",
]

Expand Down
5 changes: 5 additions & 0 deletions tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,11 @@ def test_schema_generated(api_client):
assert response.status_code == 200


def test_protected_schema_response(api_client):
response = api_client.get("/protected-schema/")
assert response.status_code == 403


@extend_schema_serializer(
examples=[
OpenApiExample(
Expand Down
5 changes: 5 additions & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.urls import path
from drf_spectacular.views import SpectacularAPIView
from rest_framework.permissions import IsAdminUser

from .views import (
AuthErrorView,
Expand All @@ -18,4 +19,8 @@
path("rate-limit-error/", RateLimitErrorView.as_view()),
path("recursion-error/", RecursionView.as_view()),
path("schema/", SpectacularAPIView.as_view(), name="api-schema"),
path(
"protected-schema/",
SpectacularAPIView.as_view(permission_classes=[IsAdminUser]),
),
]
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ python =
deps =
pytest
pytest-django
drf-spectacular>=0.27.0
drf-spectacular>=0.27.1
django-filter
dj32: Django>=3.2,<4.0
dj40: Django>=4.0,<4.1
Expand Down
Loading