Class: Minicron::Hub::ExceptionHandling

Inherits:
Object
  • Object
show all
Defined in:
lib/minicron/hub/app.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ExceptionHandling

Returns a new instance of ExceptionHandling.


102
103
104
# File 'lib/minicron/hub/app.rb', line 102

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


122
123
124
125
126
127
128
129
130
# File 'lib/minicron/hub/app.rb', line 122

def call(env)
  begin
    @app.call env
  rescue ActiveRecord::RecordNotFound => e
    handle_exception(env, e, 404)
  rescue Exception => e
    handle_exception(env, e, 500)
  end
end

#handle_exception(env, e, status) ⇒ Object


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/minicron/hub/app.rb', line 106

def handle_exception(env, e, status)
  if Minicron.config['trace']
    env['rack.errors'].puts(e)
    env['rack.errors'].puts(e.backtrace.join("\n"))
    env['rack.errors'].flush
  end

  # Display the error message
  hash = { :error => e.to_s }

  # Display the full trace if tracing is enabled
  hash[:trace] = e.backtrace if Minicron.config['trace']

  [status, { 'Content-Type' => 'application/json' }, [hash.to_json]]
end