Module: HumanError::RescuableResource

Defined in:
lib/human_error/rescuable_resource.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/human_error/rescuable_resource.rb', line 5

def self.included(base)
  base.include ResourceNaming

  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