Sha256: 5d7042282d42b80b7f0e1262d65d1fef526decd1cd5498a19f8ccc3c8a006dac

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

require 'rest-core/middleware'

class RestCore::ErrorHandler
  def self.members; [:error_handler]; end
  include RestCore::Middleware

  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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rest-core-3.4.0 lib/rest-core/middleware/error_handler.rb
rest-core-3.3.3 lib/rest-core/middleware/error_handler.rb