Sha256: ffee5413ae98ffed05d7475cc8e9b28bb72a9ef9755df4741f61f38649405758
Contents?: true
Size: 1.01 KB
Versions: 3
Compression:
Stored size: 1.01 KB
Contents
module FunWithJsonApi module Middleware class CatchJsonApiParseErrors JSON_API_REGEX = %r{application\/vnd\.api\+json} def initialize(app) @app = app end def call(env) @app.call(env) rescue ActionDispatch::ParamsParser::ParseError => error if env['CONTENT_TYPE'] =~ JSON_API_REGEX && respond_with_json_api_error?(env) build_json_api_parse_error_response else raise error end end private def build_json_api_parse_error_response title = I18n.t('fun_with_json_api.exceptions.invalid_request_body') [ 400, { 'Content-Type' => FunWithJsonApi::MEDIA_TYPE }, [ { errors: [{ code: 'invalid_request_body', title: title, status: '400' }] }.to_json ] ] end def respond_with_json_api_error?(env) FunWithJsonApi.configuration.force_render_parse_errors_as_json_api? || env['HTTP_ACCEPT'] =~ JSON_API_REGEX end end end end
Version data entries
3 entries across 3 versions & 1 rubygems