Sha256: 25f2caf958c2579f23b6955c20b36597b2629a6268599ac0f591f6e2b7704491

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

require_relative 'error/formatter'
require_relative 'error_handler/bad_request'
require_relative 'error_handler/unauthorized'

module RDStation
  class ErrorHandler
    def initialize(response)
      @response = response
      @code = response.code
    end

    def raise_error
      raise error_class, array_of_errors.first if error_class < RDStation::Error

      error_class.new(array_of_errors).raise_error
    end

    private

    attr_reader :response, :code

    def error_class
      case code
      when 400      then RDStation::ErrorHandler::BadRequest
      when 401      then RDStation::ErrorHandler::Unauthorized
      when 403      then RDStation::Error::Forbidden
      when 404      then RDStation::Error::NotFound
      when 405      then RDStation::Error::MethodNotAllowed
      when 406      then RDStation::Error::NotAcceptable
      when 409      then RDStation::Error::Conflict
      when 415      then RDStation::Error::UnsupportedMediaType
      when 422      then RDStation::Error::UnprocessableEntity
      when 500      then RDStation::Error::InternalServerError
      when 501      then RDStation::Error::NotImplemented
      when 502      then RDStation::Error::BadGateway
      when 503      then RDStation::Error::ServiceUnavailable
      when 500..599 then RDStation::Error::ServerError
      end
    end

    def array_of_errors
      error_formatter.to_array.map do |error|
        error.merge(additional_error_attributes)
      end
    end

    def response_errors
      JSON.parse(response.body)
    end

    def error_formatter
      @error_formatter = RDStation::Error::Formatter.new(response_errors)
    end

    def additional_error_attributes
      {
        'headers' => response.headers,
        'body' => JSON.parse(response.body),
        'http_status' => response.code,
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rdstation-ruby-client-2.1.0 lib/rdstation/error_handler.rb
rdstation-ruby-client-2.0.0 lib/rdstation/error_handler.rb