-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextra_handlers.py
More file actions
36 lines (29 loc) · 1.28 KB
/
extra_handlers.py
File metadata and controls
36 lines (29 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
This module contains custom error handlers for DRF exceptions.
It allows you to define additional handlers for specific exceptions
or to modify the behavior of existing exceptions.
Functions:
- `set_default_detail_to_formatted_exc_default_code`:
Formats the `default_detail` for specific DRF exceptions
by setting it to a human-readable string based on the exception `default_code`.
"""
from django.utils.translation import gettext_lazy as _
from rest_framework import exceptions
def set_default_detail_to_formatted_exc_default_code(exc: Exception) -> Exception:
"""
Formats the `default_detail` for specific DRF exceptions
by setting it to a human-readable string based on the exception `default_code`.
This ensures that the `default_detail` is consistent and descriptive, such as
'Method not allowed.' instead of using a template string, leaving the exception
`detail` to be more informative.
"""
exceptions_to_format = (
exceptions.MethodNotAllowed,
exceptions.UnsupportedMediaType,
)
if not isinstance(exc, exceptions_to_format):
return exc
# Compose the title based on the exception code.
title = exc.default_code.replace("_", " ").capitalize() + "."
exc.default_detail = _(title)
return exc