Module: RailsReactErrors::Controller

Defined in:
lib/rails_react_errors/controller.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_react_errors/controller.rb', line 5

def self.included(base)
  base.include RailsReactErrors::Renderer

  if defined?(ActiveRecord::RecordNotFound)
    base.rescue_from ActiveRecord::RecordNotFound, with: :handle_record_not_found
  end

  if defined?(ActiveRecord::RecordInvalid)
    base.rescue_from ActiveRecord::RecordInvalid, with: :handle_record_invalid
  end

  if defined?(ActionController::ParameterMissing)
    base.rescue_from ActionController::ParameterMissing, with: :handle_parameter_missing
  end

  base.rescue_from ActiveRecord::RecordNotUnique, with: :handle_conflict if defined?(ActiveRecord::RecordNotUnique)

  base.rescue_from JSON::ParserError, with: :handle_invalid_json if defined?(JSON::ParserError)

  RailsReactErrors.configuration.custom_exceptions.each do |klass, config|
    exception_class = Object.const_get(klass)
    base.rescue_from exception_class do |exception|
      render_error(
        message: exception.message,
        code: config[:code],
        status: config[:status]
      )
    end
  rescue NameError
    next
  end

  return unless RailsReactErrors.configuration.rescue_standard_error

  base.rescue_from StandardError, with: :handle_internal_server_error
end