Sha256: 448406c83e298b2cc4750a80bef761c40d4395a0c6fc957229661b68ae10e3fb

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

require 'faraday'

# @api private
module Open311
  module Response
    class RaiseError < Faraday::Response::Middleware
      def on_complete(response) # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength
        case response[:status].to_i
        when 400
          fail Open311::BadRequest.new(error_message(response))
        when 401
          fail Open311::Unauthorized.new(error_message(response))
        when 403
          fail Open311::Forbidden.new(error_message(response))
        when 404
          fail Open311::NotFound.new(error_message(response))
        when 406
          fail Open311::NotAcceptable.new(error_message(response))
        when 500
          fail Open311::InternalServerError.new(error_message(response))
        when 502
          fail Open311::BadGateway.new(error_message(response))
        when 503
          fail Open311::ServiceUnavailable.new(error_message(response))
        end
      end

      def error_message(response)
        "#{response[:method].to_s.upcase} #{response[:url]}: #{response[:response_headers]['status']}#{(': ' + response[:body]['error']) if response[:body] && response[:body]['error']}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
open311-0.3.1 lib/open311/response/raise_error.rb
open311-0.3.0 lib/open311/response/raise_error.rb