Sha256: 67894bfb2f4acece2ebd5b2ecf7ab5500b76240df6e4b6d6ddb1d389a0fe92df

Contents?: true

Size: 919 Bytes

Versions: 3

Compression:

Stored size: 919 Bytes

Contents

module EngagingNetworksRest
  module Response
    class NotFound < StandardError ; end
    class Unauthorized < StandardError ; end
    class InternalError < StandardError; end

    class RaiseError < Faraday::Response::Middleware
      def on_complete(response)
        status_code = response[:status].to_i
        if (400...600).include? status_code
          case status_code
          when 401
            raise Unauthorized.new(error_message(response))
          when 404
            raise NotFound.new(error_message(response))
          when 500
            raise InternalError.new(error_message(response))
          else
            raise StandardError.new(error_message(response))
          end
        end
      end

      def error_message(response)
        "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]} \n\n #{response[:body] if response[:body]}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
engaging-networks-rest-0.6.0 lib/engaging_networks_rest/response/raise_error.rb
engaging-networks-rest-0.5.0 lib/engaging_networks_rest/response/raise_error.rb
engaging-networks-rest-0.4.1 lib/engaging_networks_rest/response/raise_error.rb