# 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