Sha256: 6320d247b92b024ba94e41e55719895b6702959e4178d3ab83d7e30b910a9163

Contents?: true

Size: 1.04 KB

Versions: 13

Compression:

Stored size: 1.04 KB

Contents

module Locomotive
  module API

    module ExceptionRescuers

      extend ActiveSupport::Concern

      included do

        rescue_from :all do |e|
          Rails.logger.error "[API] " + e.message + "\n\t" + e.backtrace.join("\n\t")

          error!({ error: e.message }, 500)
        end

        rescue_from ::Mongoid::Errors::DocumentNotFound do
          error!({ error: 'Resource not found' }, 404)
        end

        rescue_from ::Mongoid::Errors::Validations do |e|
          error!({ error: 'Resource invalid', attributes: e.document.errors.messages }, 422)
        end

        rescue_from Grape::Exceptions::ValidationErrors do |e|
          attributes = {}.tap do |hash|
            e.errors.each { |k, v| hash[k.first.match(/\[([^\[\]]+)\]/).try(:[], 1) || k.first] = v.map { |error| error.to_s } }
          end
          error!({ error: 'Resource invalid', attributes: attributes }, 422)
        end

        rescue_from Pundit::NotAuthorizedError do
          error!({ error: 'Unauthorized' }, 401)
        end

      end

    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
locomotivecms-4.2.0.alpha2 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.2.0.alpha1 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.1.1 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.1.0 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.1.0.rc1 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.0.3 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.0.2 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.0.1 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.0.0 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.0.0.rc0 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.0.0.alpha3 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.0.0.alpha2 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-4.0.0.alpha1 app/api/locomotive/api/exception_rescuers.rb