Module: Stockpot::Helper::Errors

Included in:
MainController, RecordsController
Defined in:
app/controllers/stockpot/helper/errors.rb

Instance Method Summary collapse

Instance Method Details

#rescue_error(error) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/stockpot/helper/errors.rb', line 9

def rescue_error(error)
  logger = Logger.new($stderr)
  logger.warn(error)

  case error
  when NameError
    return_error(error.message, :bad_request, error.backtrace.first(5))
  when PG::Error
    return_error("Postgres error: #{error.message}", :internal_server_error, error.backtrace.first(5))
  when ActiveRecord::RecordInvalid, ActiveRecord::Validations, ActiveRecord::RecordNotDestroyed
    return_error(%q(In "#{error.record.class}" class: "#{error.message}"), :expectation_failed, error.backtrace.first(5))
  else
    return_error(error.message, :internal_server_error, error.backtrace.first(5))
  end
end

#return_error(message, status, backtrace = "No backtrace") ⇒ Object



25
26
27
# File 'app/controllers/stockpot/helper/errors.rb', line 25

def return_error(message, status, backtrace = "No backtrace")
  render json: { error: { status: status, backtrace: backtrace, message: message }}, status: status
end