-
Notifications
You must be signed in to change notification settings - Fork 277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exception in Core::Response when handling response from failed JSON deserialize attempt #794
Comments
I suggest adding engine.serializer.error hook. Right now in Dancer2::Core::Role::Serializer->(de)serialize the errors we catch at (de)serialization are only logged; but there is no way of processing them. I believe this is the result of a change, that allowed serializing non-ref values as well. One option that came to mind is maybe to replace this line; we could save the return value of $self->serialize and after that we could check if it is defined. If it is undef, we could fire an error, possibly calling a hook like 'route.serialization.error'. |
I've committed a fix for not crashing on modifying a read-only value. It still doesn't produce a 500, which is should. That is a separate topic. Thank you for opening the issue and supplying the testing code! |
Do you mean presetting the HTTP status code to 500 or returning a 500 response right away? In my current project I need to check the success of the deserialization and return a custom error message if input data was invalid. Please don't make it return automatically. If I can check body length and response->data that allows me checking for errors. No hook is required for me now (although ideal solution). |
If the serialization crashes, it must return a 500. The fact we don't do it now is a bug, and that needs to be fixed. You should be able to handle wrong input using the serializer.before hook. I'd be more than happy to add a test to make sure you can handle your special case. |
Removing HTTP::Body caused the issue reported in #794 to reoccur. (As HTTP::Body would process JSON content and throw an error). As Role::Serializer wraps the deserialization in an eval and always returns (undef on failure), to catch an error occuring; this locally replaces the serializer log_cb so we get a signal of the failure and die if necessary. Its, umm, well, messy and should be considered a workaround (at best).
Removing HTTP::Body caused the issue reported in #794 to reoccur. (As HTTP::Body would process JSON content and throw an error). As Role::Serializer wraps the deserialization in an eval and always returns (undef on failure), to catch an error occuring; this locally replaces the serializer log_cb so we get a signal of the failure and die if necessary. Its, umm, well, messy and should be considered a workaround (at best).
This is currently not possible. Looking at the |
I'm writing an app handling API requests and need to handle invalid JSON/YAML input with a custom error message. Unfortunately Dancer2 behaves strangely when confronted with invalid JSON data.
I built the following example which should have a server side error and send a valid HTTP error back. Instead the code fails at the client side, too, when accessing the response's content.
Output from my terminal:
How to correctly handle invalid data (after all with Serializer::Mutable) on the server side? What's wrong here that decoded_content() fails?
The text was updated successfully, but these errors were encountered: