Skip to content
Snippets Groups Projects
Commit c9164158 authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt :koala:
Browse files

feat: API errors now come with an optional code

This change allows clients to identify specific errors by an optional
code provided as part of any APIException instances raised in our code.

This code:

```py
raise ValidationError(
    "Please provide a correct date and time",
    code="time-invalid",
)
```

now returns an error object via the API with a message and a code
property making error processing on the client side easy for both humans
and machines and looks like this:

```json
{
    "message": "Please provide a correct date and time",
    "code": "time-invalid"
}
```
parent 4f7b37ea
No related branches found
No related tags found
No related merge requests found
......@@ -109,6 +109,7 @@ REST_FRAMEWORK = {
"program.auth.OidcOauth2Auth",
],
"DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"],
"EXCEPTION_HANDLER": "steering.views.full_details_exception_handler",
}
INSTALLED_APPS = (
......
from rest_framework.exceptions import APIException
from rest_framework.views import exception_handler
def full_details_exception_handler(exc, context):
if isinstance(exc, APIException):
exc.detail = exc.get_full_details()
return exception_handler(exc, context)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment