Sha256: 917e20b15b0becae21521a28ac4283194949b68a7a1a9b452b73017547359428

Contents?: true

Size: 1021 Bytes

Versions: 15

Compression:

Stored size: 1021 Bytes

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
    if res[ASYNC]
      res.merge(RESPONSE_BODY => err)
    else
      raise err
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
rest-core-3.3.2 lib/rest-core/middleware/error_handler.rb
rest-core-3.3.1 lib/rest-core/middleware/error_handler.rb
rest-core-3.3.0 lib/rest-core/middleware/error_handler.rb
rest-core-3.2.0 lib/rest-core/middleware/error_handler.rb
rest-core-3.1.1 lib/rest-core/middleware/error_handler.rb
rest-core-3.1.0 lib/rest-core/middleware/error_handler.rb
rest-core-3.0.0 lib/rest-core/middleware/error_handler.rb
rest-core-2.1.2 lib/rest-core/middleware/error_handler.rb
rest-core-2.1.1 lib/rest-core/middleware/error_handler.rb
rest-core-2.1.0 lib/rest-core/middleware/error_handler.rb
rest-core-2.0.4 lib/rest-core/middleware/error_handler.rb
rest-core-2.0.3 lib/rest-core/middleware/error_handler.rb
rest-core-2.0.2 lib/rest-core/middleware/error_handler.rb
rest-core-2.0.1 lib/rest-core/middleware/error_handler.rb
rest-core-2.0.0 lib/rest-core/middleware/error_handler.rb