Exception: IBMCloudSdkCore::ApiException

Inherits:
StandardError
  • Object
show all
Defined in:
lib/ibm_cloud_sdk_core/api_exception.rb

Overview

Custom exception class for errors returned from the APIs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code: nil, error: nil, info: nil, transaction_id: nil, global_transaction_id: nil, response: nil) ⇒ ApiException

:param HTTP::Response response: The response object from the API



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ibm_cloud_sdk_core/api_exception.rb', line 10

def initialize(code: nil, error: nil, info: nil, transaction_id: nil, global_transaction_id: nil, response: nil)
  if code.nil? || error.nil?
    @code = response.code
    @error = response.reason
    unless response.body.empty?
      body_hash = JSON.parse(response.body.to_s)
      error_message = body_hash["errors"] && body_hash["errors"][0] ? body_hash["errors"][0]["message"] : nil
      @code = body_hash["code"] || body_hash["error_code"] || body_hash["status"]
      @error = error_message || body_hash["error"] || body_hash["message"] || body_hash["errorMessage"]
      %w[code error_code status errors error message].each { |k| body_hash.delete(k) }
      @info = body_hash
    end
  else
    # :nocov:
    @code = code
    @error = error
    @info = info
    # :nocov:
  end
  @transaction_id = transaction_id || response.headers["X-Dp-Watson-Tran-Id"]
  @global_transaction_id = global_transaction_id || response.headers["X-Global-Transaction-Id"]
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



8
9
10
# File 'lib/ibm_cloud_sdk_core/api_exception.rb', line 8

def code
  @code
end

#errorObject (readonly)

Returns the value of attribute error.



8
9
10
# File 'lib/ibm_cloud_sdk_core/api_exception.rb', line 8

def error
  @error
end

#global_transaction_idObject (readonly)

Returns the value of attribute global_transaction_id.



8
9
10
# File 'lib/ibm_cloud_sdk_core/api_exception.rb', line 8

def global_transaction_id
  @global_transaction_id
end

#infoObject (readonly)

Returns the value of attribute info.



8
9
10
# File 'lib/ibm_cloud_sdk_core/api_exception.rb', line 8

def info
  @info
end

#transaction_idObject (readonly)

Returns the value of attribute transaction_id.



8
9
10
# File 'lib/ibm_cloud_sdk_core/api_exception.rb', line 8

def transaction_id
  @transaction_id
end

Instance Method Details

#to_sObject



33
34
35
36
37
38
39
# File 'lib/ibm_cloud_sdk_core/api_exception.rb', line 33

def to_s
  msg = "Error: #{@error}, Code: #{@code}"
  msg += ", Information: #{@info}" unless @info.nil?
  msg += ", X-dp-watson-tran-id: #{@transaction_id}" unless @transaction_id.nil?
  msg += ", X-global-transaction-id: #{@global_transaction_id}" unless @global_transaction_id.nil?
  msg
end