[Django] #35098: ValidationError is being converted into a 500 Internal Server Error response

16 views
Skip to first unread message

Django

unread,
Jan 9, 2024, 2:24:37 PM1/9/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
---------------------------------------------+------------------------
Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: new
Component: Error reporting | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
---------------------------------------------+------------------------
If I raise a ValidationError in my app:
https://docs.djangoproject.com/en/5.0/ref/exceptions/#validationerror

It is not explicitly handled by the function that converts exceptions into
Django responses:
https://github.com/django/django/blob/ecd3071dac9bc32028849b1563dc30e49744950e/django/core/handlers/exception.py#L63

So, the app is responding with a 500 Internal Server Error.

Expected behaviour is that it should respond with 400 Bad Request.

--
Ticket URL: <https://code.djangoproject.com/ticket/35098>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 9, 2024, 2:54:30 PM1/9/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
-----------------------------------+--------------------------------------

Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: closed

Component: Error reporting | Version: 5.0
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

Exceptions are not converted to anything if you raise them at a random
place in your app. If you're having trouble understanding how Django
works, see TicketClosingReasons/UseSupportChannels for ways to get help.

--
Ticket URL: <https://code.djangoproject.com/ticket/35098#comment:1>

Django

unread,
Jan 9, 2024, 2:57:25 PM1/9/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
-----------------------------------+--------------------------------------
Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: closed
Component: Error reporting | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Natalia Bidart):

Hello, thank you for your report and interest in making Django better.
When you say:

Expected behaviour is that it should respond with 400 Bad Request.

What do you mean exactly? Django has historically had this behavior. If
`ValidationError` is raised outside documented form clean methods, it'll
get propagated so upper layers handle it, or they will end up as a 500
response (because this is really an unhandled error).

--
Ticket URL: <https://code.djangoproject.com/ticket/35098#comment:2>

Django

unread,
Jan 9, 2024, 3:14:12 PM1/9/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
-----------------------------------+--------------------------------------
Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: closed
Component: Error reporting | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Yawar Quadir Amin):

Here's a simplified example. I have a `views.py` with:

{{{
# Obviously, this is used as a handler for a route in urls.py
def handle_foo(request: HttpRequest, **kwargs: dict[str, Any]) ->
HttpResponse:
do_something(json.loads(request.body.decode('utf-8'))['field_i_want'])
}}}

And I have `do_something` defined as:

{{{
def do_something(value: str):
if bad(value): raise ValidationError('bad value')
return f'good value: {value}'
}}}

Now I expect this to respond with a 400 Bad Request, but it responds with
500 Internal Server Error. And the traceback points to the function in
Django Core which I linked earlier. It seems like this function is
supposed to handle Django exceptions, so I'm wondering why it doesn't
handle the ValidationException?

--
Ticket URL: <https://code.djangoproject.com/ticket/35098#comment:3>

Django

unread,
Jan 9, 2024, 3:21:17 PM1/9/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
-----------------------------------+--------------------------------------
Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: closed
Component: Error reporting | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Yawar Quadir Amin):

Exceptions are not converted to anything if you raise them at a random
place in your app.

(a) It's not a random place, it's directly in the call stack of the URL
route handler.

(b) Is this documented? I'm looking at the recommended best practices:
https://docs.djangoproject.com/en/5.0/ref/forms/validation/#raising-
validation-error

And it only shows you how to raise the error yourself. It doesn't say
anything about raising it in a specific place?

--
Ticket URL: <https://code.djangoproject.com/ticket/35098#comment:4>

Django

unread,
Jan 9, 2024, 3:30:41 PM1/9/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
-----------------------------------+--------------------------------------
Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: closed
Component: Error reporting | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Mariusz Felisiak):

Again, Trac **is not** a support channel. If you're having trouble


understanding how Django works, see
TicketClosingReasons/UseSupportChannels for ways to get help.

--
Ticket URL: <https://code.djangoproject.com/ticket/35098#comment:5>

Django

unread,
Jan 9, 2024, 3:52:04 PM1/9/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
-----------------------------------+--------------------------------------
Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: closed
Component: Error reporting | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Yawar Quadir Amin):

Hi, I am not interested in getting support. I can handle the error
perfectly fine on my end. I am reporting what seems like an obvious bug
with an easy fix. You're not interested, that's fine. I am good either
way.

--
Ticket URL: <https://code.djangoproject.com/ticket/35098#comment:6>

Django

unread,
Jan 9, 2024, 4:03:00 PM1/9/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
-----------------------------------+--------------------------------------
Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: closed
Component: Error reporting | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Natalia Bidart):

Replying to [comment:6 Yawar Quadir Amin]:


> Hi, I am not interested in getting support. I can handle the error
perfectly fine on my end. I am reporting what seems like an obvious bug
with an easy fix. You're not interested, that's fine. I am good either
way.

We define support as understanding how Django operates and the expected
patterns for use in your Django app. Numerous tutorials and documentation
resources are available to guide you in encapsulating your app's business
logic into specific modules and forms. It's important to note that failing
to handle a `ValidationError` in your code will result in a 500 response,
which is a standard behavior in Django and not a bug.

--
Ticket URL: <https://code.djangoproject.com/ticket/35098#comment:7>

Django

unread,
Jan 9, 2024, 4:48:54 PM1/9/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
-----------------------------------+--------------------------------------
Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: closed
Component: Error reporting | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Yawar Quadir Amin):

Respectfully, a great place to document this would be
https://docs.djangoproject.com/en/5.0/ref/exceptions/

Something like:

> Django raises some of its own exceptions as well as standard Python
exceptions. All exceptions cause a 500 Internal Server Error response if
they reach the handler level, unless documented otherwise.

And then actually documenting the specific exceptions which are processed
and return non-500 responses.

--
Ticket URL: <https://code.djangoproject.com/ticket/35098#comment:8>

Django

unread,
Jan 10, 2024, 12:09:20 AM1/10/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
-----------------------------------+--------------------------------------
Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: closed
Component: Error reporting | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Mariusz Felisiak):

This is not a bug but a standard behavior not only for Django but for any
framework or programming language. There is nothing to document.
Personally, I'd recommend reaching support channels and trying to
understand Django better before writing more code, but of course it's your
choice. All the best.

--
Ticket URL: <https://code.djangoproject.com/ticket/35098#comment:9>

Django

unread,
Jan 10, 2024, 10:49:07 AM1/10/24
to django-...@googlegroups.com
#35098: ValidationError is being converted into a 500 Internal Server Error
response
-----------------------------------+--------------------------------------
Reporter: Yawar Quadir Amin | Owner: (none)
Type: Bug | Status: closed
Component: Error reporting | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Yawar Quadir Amin):

As I mentioned yesterday, I already solved the issue on my end with a
couple lines of code changes. Thanks for your concern. I am happy to read
any documentation but it seems there is nothing to document because this
is a universal behaviour. Yet when I raise a `RequestValidationError` in a
FastAPI handler, it is converted into a 422 Unprocessable Entity response.

Since this is fine, nothing further from my side. Thanks for your
attention.

--
Ticket URL: <https://code.djangoproject.com/ticket/35098#comment:10>

Reply all
Reply to author
Forward
0 new messages