Skip to content

Commit 77c790f

Browse files
committed
change ErrorType from an enum to a type hint and replace the enum members use cases with constants.
This avoids triggering an exception when returning a yaml response since the yaml renderer used by drf-spectacular does not handle enums by default fixes #98
1 parent 21304d9 commit 77c790f

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

docs/changelog.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ message:
3535
}
3636
```
3737
- set minimum version of drf-spectacular to 0.27.1
38+
- `drf_standardized_errors.types.ErrorType` is now the following type hint
39+
```python
40+
from typing import Literal
41+
ErrorType = Literal["validation_error", "client_error", "server_error"]
42+
```
43+
`ErrorType` was previously an enum. If you referenced its members in your code, make sure to replace their
44+
use cases with the newly added constants:
45+
```
46+
from drf_standardized_errors.types import VALIDATION_ERROR, CLIENT_ERROR, SERVER_ERROR
47+
ErrorType.VALIDATION_ERROR --> VALIDATION_ERROR
48+
ErrorType.CLIENT_ERROR --> CLIENT_ERROR
49+
ErrorType.SERVER_ERROR --> SERVER_ERROR
50+
```
3851

3952
## [0.14.1] - 2024-08-10
4053
### Added

drf_standardized_errors/formatter.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
55
from rest_framework.status import is_client_error
66

77
from .settings import package_settings
8-
from .types import Error, ErrorResponse, ErrorType, ExceptionHandlerContext
8+
from .types import (
9+
CLIENT_ERROR,
10+
SERVER_ERROR,
11+
VALIDATION_ERROR,
12+
Error,
13+
ErrorResponse,
14+
ErrorType,
15+
ExceptionHandlerContext,
16+
)
917

1018

1119
class ExceptionFormatter:
@@ -42,11 +50,11 @@ def run(self) -> Any:
4250

4351
def get_error_type(self) -> ErrorType:
4452
if isinstance(self.exc, exceptions.ValidationError):
45-
return ErrorType.VALIDATION_ERROR
53+
return VALIDATION_ERROR
4654
elif is_client_error(self.exc.status_code):
47-
return ErrorType.CLIENT_ERROR
55+
return CLIENT_ERROR
4856
else:
49-
return ErrorType.SERVER_ERROR
57+
return SERVER_ERROR
5058

5159
def get_errors(self) -> List[Error]:
5260
"""

drf_standardized_errors/types.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from dataclasses import dataclass
2-
from enum import Enum
3-
from typing import List, Optional, TypedDict
2+
from typing import Final, List, Literal, Optional, TypedDict
43

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

1514

16-
class ErrorType(str, Enum):
17-
VALIDATION_ERROR = "validation_error"
18-
CLIENT_ERROR = "client_error"
19-
SERVER_ERROR = "server_error"
15+
VALIDATION_ERROR: Final = "validation_error"
16+
CLIENT_ERROR: Final = "client_error"
17+
SERVER_ERROR: Final = "server_error"
18+
ErrorType = Literal["validation_error", "client_error", "server_error"]
2019

2120

2221
@dataclass

0 commit comments

Comments
 (0)