Module: Herbert::Error

Defined in:
lib/herbert/ApplicationError.rb,
lib/herbert/Error.rb

Overview

Provides centralized handling of exceptions in an application context

Defined Under Namespace

Modules: Helpers Classes: ApplicationError

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Inclusion hook



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
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/herbert/Error.rb', line 8

def self.registered(app)
  # Disable HTML errors and preliminary reporting
  log.h_warn("Herbert is running in debugging mode - exceptions will be visualized") if app.debug?
  app.set :raise_errors, false
  app.set :show_exceptions, false
  app.set :dump_errors, app.debug?
  # Add a new error state handler which produces
  # compact JSON error reports (handled by #Sinatra::Jsonify)
  app.error do
    err = request.env['sinatra.error']
    if err.class == ApplicationError then
      log.h_debug("Caught manageable error")
      response.status = err.http_code
      body = {
        :error => {
          :code => err.code,
          :message => err.message
        }
      }
      # Add backtrace, Kwalify validation report and other info if
      # running in development mode
      if (settings.development? || settings.test?) then
        log.h_debug("Adding stacktrace and report to the error")
        body[:error][:stacktrace] = err.backtrace.join("\n")
        body[:error][:info] = (err.errors || [])
      end
      response.body = body
    else
    # If the exception is not manageable, bust it
      log.h_error("A non-managed error occured! Backtrace: #{err.to_s + err.backtrace.join("\n")}")
      response.status = 500
      response.body = (settings.development? || settings.test?) ? err.to_s : nil
    end
  end

  #Ummm, nasty.... FIXME
  app.not_found do
    content_type 'application/json', :charset => 'utf-8'
    {:error => {
      :code => 1003,
      :message => "Not found"
    }}
  end
end