Last active
July 20, 2023 06:44
-
-
Save ArturT/616bd7c1f6b823b312e7f8c4ae3ba8ee to your computer and use it in GitHub Desktop.
How to rescue ActionDispatch::Http::MimeNegotiation::InvalidType in API controller for Rails 6.1+ and render nice JSON error. Learn more how you could run RSpec/Minitest tests faster in Rails app https://docs.knapsackpro.com/2020/how-to-speed-up-ruby-and-javascript-tests-with-ci-parallelisation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is example how to rescue from exception ActionDispatch::Http::MimeNegotiation::InvalidType | |
# and show nice JSON error in your API | |
module API | |
class BaseController < ActionController::API | |
def process_action(*args) | |
super | |
rescue ActionDispatch::Http::MimeNegotiation::InvalidType => exception | |
# set valid Content-Type to be able to call render method below | |
request.headers['Content-Type'] = 'application/json' | |
render status: 400, json: { errors: [exception.message] } | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This if full example for your API in Rails 6.1+ that will render errors as JSON instead of ugly HTML error page | |
module API | |
class BaseController < ActionController::API | |
def process_action(*args) | |
super | |
rescue ActionDispatch::Http::Parameters::ParseError => exception | |
# https://github.com/rails/rails/issues/34244#issuecomment-433365579 | |
render status: 400, json: { errors: [exception.message] } | |
rescue ActionDispatch::Http::MimeNegotiation::InvalidType => exception | |
# set valid Content-Type to be able to call render method below | |
request.headers['Content-Type'] = 'application/json' | |
render status: 400, json: { errors: [exception.message] } | |
end | |
# This will work for Rails > 5.2.2 | |
# https://github.com/rails/rails/pull/34341#issuecomment-434727301 | |
rescue_from ActionDispatch::Http::Parameters::ParseError do |exception| | |
render status: 400, json: { errors: [exception.cause.message] } | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This if full example for your API in Rails < 6.1 that will render errors as JSON instead of ugly HTML error page | |
module API | |
class BaseController < ActionController::API | |
def process_action(*args) | |
super | |
rescue ActionDispatch::Http::Parameters::ParseError => exception | |
# https://github.com/rails/rails/issues/34244#issuecomment-433365579 | |
render status: 400, json: { errors: [exception.message] } | |
rescue Mime::Type::InvalidMimeType => exception | |
render status: 400, json: { errors: [exception.message] } | |
end | |
# This will work for Rails > 5.2.2 | |
# https://github.com/rails/rails/pull/34341#issuecomment-434727301 | |
rescue_from ActionDispatch::Http::Parameters::ParseError do |exception| | |
render status: 400, json: { errors: [exception.cause.message] } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you find this gist useful please check my ruby gem knapsack_pro for running fast automated tests in CI with parallel jobs: https://knapsackpro.com/?utm_source=github&utm_medium=gist&utm_campaign=gist-actiondispatch-http-mimenegotiation-invalidtype