Sha256: 818f06d79348b7a93d1d2435e387f5cbff1723db1e22886082c3923014adbf1b

Contents?: true

Size: 1.26 KB

Versions: 14

Compression:

Stored size: 1.26 KB

Contents

module RockRMS
  class Error < StandardError; end
  class BadGateway < Error; end
  class BadRequest < Error; end
  class Forbidden < Error; end
  class GatewayTimeout < Error; end
  class InternalServerError < Error; end
  class NotFound < Error; end
  class ServiceUnavailable < Error; end
  class Unauthorized < Error; end
end

require 'faraday'
module FaradayMiddleware
  class RockRMSErrorHandler < Faraday::Response::Middleware
    ERROR_STATUSES = 400..600

    def on_complete(env)
      case env[:status]
      when 400
        raise RockRMS::BadRequest, error_message(env)
      when 401
        raise RockRMS::Unauthorized, error_message(env)
      when 403
        raise RockRMS::Forbidden, error_message(env)
      when 404
        raise RockRMS::NotFound, error_message(env)
      when 500
        raise RockRMS::InternalServerError, error_message(env)
      when 502
        raise RockRMS::BadGateway, error_message(env)
      when 503
        raise RockRMS::ServiceUnavailable, error_message(env)
      when 504
        raise RockRMS::GatewayTimeout, error_message(env)
      when ERROR_STATUSES
        raise RockRMS::Error, error_message(env)
      end
    end

    private

    def error_message(env)
      "#{env[:status]}: #{env[:url]} #{env[:body]}"
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rock_rms-7.0.3 lib/rock_rms/error.rb
rock_rms-7.0.2 lib/rock_rms/error.rb
rock_rms-7.0.1 lib/rock_rms/error.rb
rock_rms-7.0.0 lib/rock_rms/error.rb
rock_rms-6.2.0 lib/rock_rms/error.rb
rock_rms-6.1.0 lib/rock_rms/error.rb
rock_rms-6.0.8 lib/rock_rms/error.rb
rock_rms-6.0.7 lib/rock_rms/error.rb
rock_rms-6.0.6 lib/rock_rms/error.rb
rock_rms-6.0.5 lib/rock_rms/error.rb
rock_rms-6.0.4 lib/rock_rms/error.rb
rock_rms-6.0.3 lib/rock_rms/error.rb
rock_rms-6.0.1 lib/rock_rms/error.rb
rock_rms-6.0.0 lib/rock_rms/error.rb