Skip to content

Instantly share code, notes, and snippets.

@ArturT
Last active July 20, 2023 06:44
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
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 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 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 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
@ArturT
Copy link
Author

ArturT commented Jan 22, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment