Sha256: e521e1c5c320e996f07232b119ac92538b59e7db15bfbc7a3ab0dc6c780c3243

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

module MercuryWebParser
  class Error < StandardError
    # Raised when Mercury returns a 4xx or 500 HTTP status code
    class ClientError < Error
      # Creates a new error from an HTTP environement
      #
      # @param response [Hash]
      # @return [MercuryWebParser::Error::ClientError]
      def initialize(error = nil)
        http_error = error.response[:status].to_i

        if ERROR_MAP.key?(http_error)
          raise ERROR_MAP[http_error], error.response[:body]['message']
        end

        super
      end
    end # ClientError

    class ConfigurationError < MercuryWebParser::Error; end

    # Raised when there's an error in Faraday
    class RequestError < MercuryWebParser::Error; end

    # Raised when MercuryWebParser returns a 400 HTTP status code
    class BadRequest < MercuryWebParser::Error; end

    # Raised when the response from the parser is null
    class NotParseable < MercuryWebParser::Error; end

    # Raised when MercuryWebParser returns a 401 HTTP status code
    class UnauthorizedRequest < MercuryWebParser::Error; end

    # Raised when MercuryWebParser returns a 403 HTTP status code
    class Forbidden < MercuryWebParser::Error; end

    # Raised when MercuryWebParser returns a 404 HTTP status code
    class NotFound < MercuryWebParser::Error; end

    # Raised when MercuryWebParser returns a 500 HTTP status code
    class InternalServerError < MercuryWebParser::Error; end

    ERROR_MAP = {
      400 => MercuryWebParser::Error::BadRequest,
      401 => MercuryWebParser::Error::UnauthorizedRequest,
      403 => MercuryWebParser::Error::Forbidden,
      404 => MercuryWebParser::Error::NotFound,
      500 => MercuryWebParser::Error::InternalServerError
    }.freeze
  end # Error
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mercury_web_parser-0.1.2 lib/mercury_web_parser/error.rb
mercury_web_parser-0.1.0 lib/mercury_web_parser/error.rb