Skip to content
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

Error handling for JSON::ParseException not working if request with invalid JSON body #852

Closed
ucibar opened this issue Jul 16, 2019 · 5 comments
Labels

Comments

@ucibar
Copy link

ucibar commented Jul 16, 2019

Hi, I'm using crystal 0.29.0 and lucky 0.15.1.

If I send a POST request with invalid JSON body, lucky response with 500. But there is a handler method for JSON::ParseException in the actions/errors/show.cr file and lucky not trigger the method. And if I delete the handler for JSON::ParseException, It must trigger default handler for unhandled exceptions but It isn't trigger any handler.

Example:
Let's say we have a action named Bug::Create:
lucky gen.action.api Bug::Create

class Bug::Create < ApiAction
  route do
    text "Render something in Bug::Create"
  end
end

And we have error handler for JSON::ParseException:

 def handle_error(error : JSON::ParseException)
    message = "There was a problem parsing the JSON." +
              " Please check that it is formed correctly"

    json Errors::ShowSerializer.new(message), status: 400
  end

And we send a POST request with invalid JSON, e.g. {"foo"="bar"} and we got 500 and
Unhandled exception on HTTP::Handler: Unexpected char '=' at 2:12 (JSON::ParseException).

But if you doing JSON.Parse operations in your actions/forms/models and if there is a invalid JSON, JSON::ParseException perfectly captured by error handler method. For example:

class Bug::Index < ApiAction
  route do
    JSON.parse("{\"foo\"=\"bar\"}") # Invalid JSON
    text "Render something in Bug::Index"
  end
end

And JSON::ParseException will be captured by error handler.

Sorry for bad English and language mistakes.

@ucibar
Copy link
Author

ucibar commented Jul 17, 2019

I put ErrorHandler before the HttpMethodOverrideHandler in my src/app_server.cr after #854 and its worked. But I think it's should be default behavior when lucky-cli generate a project.

@jwoertink
Copy link
Member

Yeah, I'm thinking as long as HttpMethodOverrideHandler is before the RouterHandler, it should be ok to move it. We'll probably need to test a few things, but good catch!

@jwoertink jwoertink added the bug label Jul 18, 2019
jwoertink pushed a commit that referenced this issue Jul 18, 2019
…#854)

Because part of this handler looks at the request body, it can fail if the json is malformed.  I believe this is totally acceptable behavior.  The reason I point this out is because in lucky-cli, this handler is put before the ErrorHandler ([reference](https://github.com/luckyframework/lucky_cli/blob/master/src/web_app_skeleton/src/app_server.cr.ecr\#L5-L15)) when it probably shouldn't be. This is most likely the cause of #852
@paulcsmith
Copy link
Member

Sorry to chime in late. I think HttpMethodOverrideHandler should be before ErrorHandler because we need to make sure the http method is correct. I think what we should do is rescue json parse exceptions in the method override handler or something. Bit busy right now, but will try to tackle this later

@paulcsmith
Copy link
Member

and thank you for reporting this @ucibar and for investigating the cause. That was extremely helpful!

@paulcsmith
Copy link
Member

Closed by ed1e51b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants