Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. class CatchJsonParseErrors
  2. def initialize(app)
  3. @app = app
  4. end
  5.  
  6. def call(env)
  7. begin
  8. @app.call(env)
  9. rescue ActionDispatch::ParamsParser::ParseError => error
  10. if env['HTTP_ACCEPT'] =~ /application/json/
  11. error_output = "There was a problem in the JSON: #{error}"
  12. return [
  13. 400, { "Content-Type" => "application/json" },
  14. [ { status: 400, error: error_output }.to_json ]
  15. ]
  16. else
  17. raise error
  18. end
  19. end
  20. end
  21. end
  22.  
  23. config.middleware.insert_before ActionDispatch::ParamsParser, "CatchJsonParseErrors"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement