Exception: AftershipAPI::ApiError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/aftership-tracking-sdk/error.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil) ⇒ ApiError

Usage examples:

ApiError.new
ApiError.new("message")
ApiError.new(:status_code => 500, :response_headers => {}, :response_body => "")
ApiError.new(:status_code => 404, :message => "Not Found")


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aftership-tracking-sdk/error.rb', line 75

def initialize(arg = nil)
  if arg.is_a? Hash
    if arg.key?(:message) || arg.key?('message')
      super(arg[:message] || arg['message'])
    else
      super arg
    end

    response_body = JSON.parse(arg[:response_body], :symbolize_names => true) rescue nil
    @message = REQUEST_ERROR
    if response_body && response_body[:meta] && response_body[:meta][:code]
      @meta_code = response_body[:meta][:code]
      @error_code = ERROR_MAP[@meta_code] || REQUEST_ERROR
      @message = response_body[:meta][:message] || REQUEST_ERROR
    end
    arg.each do |k, v|
      instance_variable_set "@#{k}", v
    end
  else
    super arg
    @message = arg
  end
end

Instance Attribute Details

#error_codeObject (readonly)

Returns the value of attribute error_code.



68
69
70
# File 'lib/aftership-tracking-sdk/error.rb', line 68

def error_code
  @error_code
end

#meta_codeObject (readonly)

Returns the value of attribute meta_code.



68
69
70
# File 'lib/aftership-tracking-sdk/error.rb', line 68

def meta_code
  @meta_code
end

#response_bodyObject (readonly)

Returns the value of attribute response_body.



68
69
70
# File 'lib/aftership-tracking-sdk/error.rb', line 68

def response_body
  @response_body
end

#response_headersObject (readonly)

Returns the value of attribute response_headers.



68
69
70
# File 'lib/aftership-tracking-sdk/error.rb', line 68

def response_headers
  @response_headers
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



68
69
70
# File 'lib/aftership-tracking-sdk/error.rb', line 68

def status_code
  @status_code
end

Instance Method Details

#messageObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/aftership-tracking-sdk/error.rb', line 104

def message
  if @message.nil?
    msg = "Error message: the server returns an error"
  else
    msg = @message
  end

  msg += "\nHTTP status code: #{status_code}" if status_code
  msg += "\nError Code #{error_code}" if error_code
  msg += "\nResponse headers: #{response_headers}" if response_headers
  msg += "\nResponse body: #{response_body}" if response_body

  msg
end

#to_sObject

Override to_s to display a friendly error message



100
101
102
# File 'lib/aftership-tracking-sdk/error.rb', line 100

def to_s
  message
end