Sha256: d2aa96b7882f473054d5739800f893104c4c93809a1bb194dda0ae57cd8b6c9c

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Locomotive
  module API

    module ExceptionRescuers

      extend ActiveSupport::Concern

      included do

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

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

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

        rescue_from ::Mongoid::Errors::Validations do |e|
          error_response(message: { error: 'Resource invalid', attributes: e.document.errors.messages }, status: 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_response(message: { error: 'Resource invalid', attributes: attributes }, status: 422)
        end

        rescue_from Pundit::NotAuthorizedError do
          error_response(message: { error: 'Unauthorized' }, status: 401)
        end

      end

    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotivecms-3.0.0.rc2 app/api/locomotive/api/exception_rescuers.rb
locomotivecms-3.0.0.rc1 app/api/locomotive/api/exception_rescuers.rb