Sha256: cd6a26741c6c4ec8b2836d85478d7a311a88fe7cd5b1dae91605f962c7ccd98f

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module Justa
  class JustaError < StandardError
  end

  class ConnectionError < JustaError
    attr_reader :error

    def initialize(error)
      @error = error
      super error.message
    end
  end

  class RequestError < JustaError
  end

  class ResponseError < JustaError
    attr_reader :request_params, :error

    def initialize(request_params, error, message=nil)
      @request_params, @error = request_params, error
      msg = @error.message
      msg +=  " => " + message if message
      super msg
    end
  end

  class NotFound < ResponseError
    attr_reader :response
    def initialize(response, request_params, error)
      @response = response
      super request_params, error
    end
  end

  class ValidationError < JustaError
    attr_reader :response, :errors

    def initialize(response)
      @response = response
      @errors   = response['message']&.map do |message|
        params = error.values_at('message', 'parameter_name', 'type', 'url')
        ParamError.new(*params)
      end
      super @errors&.map(&:message).join(', ')
    end

    def to_h
      @errors.map(&:to_h)
    end
  end

  class MissingCredentialsError < JustaError
  end


  class ParamError < JustaError
    attr_reader :parameter_name, :type, :url

    def initialize(message, parameter_name, type, url=nil)
      @parameter_name, @type, @url = parameter_name, type, url
      super message
    end

    def to_h
      { parameter_name: parameter_name, type: type, message: message }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
justa-ruby-0.1.9 lib/justa/errors.rb