Module: HumanError::RescuableResource

Defined in:
lib/human_error/rescuable_resource.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/human_error/rescuable_resource.rb', line 19

def self.included(base)
  base.extend ClassMethods

  base.rescue_from 'ActiveRecord::RecordInvalid',
                   'ActiveRecord::RecordNotSaved',
                   'ActiveRecord::RecordNotFound',
                   'ActiveRecord::InvalidForeignKey',
                   'ActionController::ParameterMissing',
                   'ActionController::UnpermittedParameters' do |exception|
    human_error = HumanError.convert(exception,
                                     resource_name: self.class.singular_resource_name,
                                     action:        action_name)

    render json:   human_error,
           status: human_error.http_status
  end

  base.rescue_from 'HumanError::Error' do |exception|
    render json:   exception,
           status: exception.http_status
  end
end