Sha256: c306f18860ec82d8047283b628f9c1889579b627a59036f7eadf4927a0abe139

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

module Commons
  module Errors
    module DefaultHandling
      def self.included(cls)
        cls.class_eval do
          # WARNING
          # Avoid setting rescue_from Exception/StandardError since it will override your
          # own custom rescue_from handlers https://stackoverflow.com/a/9121054/3287738
          # WARNING
          rescue_from ActiveRecord::RecordNotFound do |e|
            respond ResourceNotFound.new e.message,
                                         meta: {
                                           model: e.model,
                                           id: e.id,
                                           primary_key: e.primary_key
                                         }
          end

          rescue_from ActiveRecord::RecordInvalid do |e|
            respond InvalidResource.new e.message,
                                        validation_errors: e&.record&.errors&.to_hash
          end

          rescue_from ArgumentError do |e|
            respond InvalidResource.new e.message
          end

          rescue_from ActiveRecord::RecordNotUnique do |e|
            respond NotUnique.new e.message
          end

          rescue_from ActionController::ParameterMissing do |e|
            respond MissingParameter.new e.message, param: e.param
          end

          rescue_from ActionController::RoutingError do |e|
            respond RouteNotFound.new e.message,
                                      detail: { failures: e.failures }
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
commons_yellowme-0.16.0 lib/commons/errors/default_handling.rb
commons_yellowme-0.15.0 lib/commons/errors/default_handling.rb
commons_yellowme-0.12.0 lib/commons/errors/default_handling.rb
commons_yellowme-0.11.3 lib/commons/errors/default_handling.rb
commons_yellowme-0.11.2 lib/commons/errors/default_handling.rb
commons_yellowme-0.11.1 lib/commons/errors/default_handling.rb
commons_yellowme-0.11.0 lib/commons/errors/default_handling.rb