Exception: Miasma::Error::ApiError

Inherits:
Miasma::Error show all
Defined in:
lib/miasma/error.rb

Overview

Api related errors

Direct Known Subclasses

AuthenticationError, RequestError

Defined Under Namespace

Classes: AuthenticationError, RequestError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, args = {}) ⇒ ApiError

Create new API error instance

Options Hash (args):

  • :response (HTTP::Response)

    response from request



29
30
31
32
33
34
# File 'lib/miasma/error.rb', line 29

def initialize(msg, args={})
  super
  @response = args.to_smash[:response]
  @message = msg
  extract_error_message(@response)
end

Instance Attribute Details

#responseHTTP::Response (readonly)



20
21
22
# File 'lib/miasma/error.rb', line 20

def response
  @response
end

#response_error_msgString (readonly)



22
23
24
# File 'lib/miasma/error.rb', line 22

def response_error_msg
  @response_error_msg
end

Instance Method Details

#extract_error_message(response) ⇒ String, NilClass

Attempt to extract error message from response



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/miasma/error.rb', line 45

def extract_error_message(response)
  begin
    begin
      content = MultiJson.load(response.body.to_s).to_smash
      @response_error_msg = [[:error, :message]].map do |path|
        if(result = content.get(*path))
          "#{content[:code]}: #{result}"
        end
      end.flatten.compact.first
    rescue MultiJson::ParseError
      begin
        content = MultiXml.parse(response.body.to_s).to_smash
        @response_error_msg = [['ErrorResponse', 'Error'], ['Error']].map do |path|
          if(result = content.get(*path))
            "#{result['Code']}: #{result['Message']}"
          end
        end.compact.first
      rescue MultiXml::ParseError
        content = Smash.new
      end
    rescue
      # do nothing
    end
  end
  @response_error_msg
end

#messageString



37
38
39
# File 'lib/miasma/error.rb', line 37

def message
  [@message, @response_error_msg].compact.join(' - ')
end