Module: InstantApi::Controller::ExceptionHandler

Extended by:
ActiveSupport::Concern
Defined in:
lib/instant_api/controller/exception_handler.rb

Instance Method Summary collapse

Instance Method Details

#exception_to_json(exception) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/instant_api/controller/exception_handler.rb', line 28

def exception_to_json(exception)
  hash =  case exception
          when ActiveRecord::RecordNotFound
            {field: :id, message: exception.message}
          when ActiveRecord::RecordInvalid
            exception.record.errors.messages.to_a
          end
  { errors: hash }
end

#record_not_found(exception) ⇒ Object



16
17
18
# File 'lib/instant_api/controller/exception_handler.rb', line 16

def record_not_found(exception)
  render json: exception_to_json(exception), status: 404
end

#render_error(exception) ⇒ Object



20
21
22
# File 'lib/instant_api/controller/exception_handler.rb', line 20

def render_error(exception)
  render json: exception, status: 500
end

#rescue_record_invalid(exception) ⇒ Object



24
25
26
# File 'lib/instant_api/controller/exception_handler.rb', line 24

def rescue_record_invalid(exception)
  render json: exception_to_json(exception), status: 422
end