Sha256: 0ee45d50feff99135721624fb2721128f61cfde3e45b27450a1364530feaa128

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

module InfinumJsonApiSetup
  module JsonApi
    module ErrorHandling
      extend ActiveSupport::Concern

      included do # rubocop:disable Metrics/BlockLength
        rescue_from ActionController::ParameterMissing do |e|
          message = e.to_s.split("\n").first
          render_error(InfinumJsonApiSetup::Error::BadRequest.new(message: message))
        end

        rescue_from ActiveRecord::RecordNotFound do
          render_error(InfinumJsonApiSetup::Error::RecordNotFound.new)
        end

        if defined?(Pundit)
          rescue_from Pundit::NotAuthorizedError do
            render_error(InfinumJsonApiSetup::Error::Forbidden.new)
          end
        end

        if defined?(Jsonapi::QueryBuilder)
          rescue_from Jsonapi::QueryBuilder::Mixins::Sort::UnpermittedSortParameters do |e|
            Bugsnag.notify(e)
            render_error(InfinumJsonApiSetup::Error::BadRequest.new(message: e.to_s))
          end
        end

        # we don't want to expose internal details to API consumer
        rescue_from PG::Error do |e|
          Bugsnag.notify(e)
          render_error(InfinumJsonApiSetup::Error::InternalServerError.new)
        end

        rescue_from I18n::InvalidLocale do |e|
          render_error(InfinumJsonApiSetup::Error::BadRequest.new(message: e.to_s))
        end

        rescue_from ActionDispatch::Http::Parameters::ParseError do |e|
          render_error(InfinumJsonApiSetup::Error::BadRequest.new(message: e.to_s))
        end
      end

      def render_error(error)
        render json_api: error, status: error.http_status
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
infinum_json_api_setup-0.0.4 lib/infinum_json_api_setup/json_api/error_handling.rb
infinum_json_api_setup-0.0.3 lib/infinum_json_api_setup/json_api/error_handling.rb