Class: RestCore::ErrorHandler

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
lib/rest-core/middleware/error_handler.rb

Constant Summary

Constants included from Middleware

Middleware::UNRESERVED

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, Simple, TIMER, Universal, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Middleware

contain_binary?, #contain_binary?, escape, #escape, #fail, #id, included, #log, merge_hash, #merge_hash, #percent_encode, percent_encode, request_uri, #request_uri, #run, #string_keys, string_keys

Methods included from RestCore

eagerload, id

Class Method Details

.membersObject



5
# File 'lib/rest-core/middleware/error_handler.rb', line 5

def self.members; [:error_handler]; end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rest-core/middleware/error_handler.rb', line 8

def call env
  app.call(env){ |res|
    yield(if (res[FAIL] || []).empty? # no errors at all
            res
          else
            # if there's an exception, hand it over
            if err = res[FAIL].find{ |e| e.kind_of?(Exception) }
              process(res, err)

            elsif h = error_handler(res)
              # if the user provides an exception, hand it over
              if (err = h.call(res)).kind_of?(Exception)
                process(res, err)

              else # otherwise we report all of them
                res.merge(FAIL => [res[FAIL], err].flatten.compact)

              end
            else # no exceptions at all, then do nothing
              res
            end
          end)}
end

#process(res, err) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rest-core/middleware/error_handler.rb', line 32

def process res, err
  RC::Promise.set_backtrace(err)
  if res[ASYNC]
    if res[HIJACK]
      res.merge(RESPONSE_SOCKET => err)
    else
      res.merge(RESPONSE_BODY => err)
    end
  else
    raise err
  end
end