Exception: Jamf::Connection::APIError

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/jamf/api/connection/api_error.rb

Overview

TODO: figure out what to do with ConflictError, BadRequestError, APIRequestError and maybe AuthenticationError

Constant Summary collapse

RSRC_NOT_FOUND =
'Resource Not Found'.freeze
ErrorInfo =

Struct to hold the info for each individual error in an API error response body.

Has these attributes (descriptions from developer.jamf.com):

code: String

Error-specific code that can be used to identify localization
string, etc.

field: String

Name of the field that caused the error.

description: String

A general description of error for troubleshooting/debugging.
Generally this text should not be displayed to a user; instead
refer to errorCode and its localized text

id: Integer

id of object with error. 0 if not applicable
ImmutableStruct.new(:code, :field, :description, :id) do
  def to_s
    deets = "{code: #{code}"
    deets << ", field: #{field}" if field
    deets << ", id: #{id}" if id
    deets << '}'
    "#{description} #{deets}"
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ APIError

Returns a new instance of APIError.

Parameters:

  • rest_error (RestClient::ExceptionWithResponse)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jamf/api/connection/api_error.rb', line 83

def initialize(http_response)
  @http_response = http_response
  @httpStatus = http_response.status
  @errors =
    if @http_response.body.is_a? String
      JSON.parse(@http_response.body)[:errors]
    elsif @http_response.body.is_a?(Hash)
      @http_response.body[:errors]
    end
  @errors &&= @errors.map { |e| ErrorInfo.new e }

  unless @errors
    code = @httpStatus
    desc = code == 404 ? RSRC_NOT_FOUND : @http_response.reason_phrase
    @errors = [ErrorInfo.new(code: code, field: nil, description: desc, id: nil)]
  end

  super
end

Instance Attribute Details

#errorsArray<ErrorInfo> (readonly)

Returns see ErrorInfo above.

Returns:



77
78
79
# File 'lib/jamf/api/connection/api_error.rb', line 77

def errors
  @errors
end

#http_responseFaraday::Response (readonly)

Returns:

  • (Faraday::Response)


68
69
70
# File 'lib/jamf/api/connection/api_error.rb', line 68

def http_response
  @http_response
end

#httpStatusinteger (readonly) Also known as: status

Returns:

  • (integer)


72
73
74
# File 'lib/jamf/api/connection/api_error.rb', line 72

def httpStatus
  @httpStatus
end

#rest_errorRestClient::ExceptionWithResponse (readonly)

Returns the original RestClient error.

Returns:

  • (RestClient::ExceptionWithResponse)

    the original RestClient error



80
81
82
# File 'lib/jamf/api/connection/api_error.rb', line 80

def rest_error
  @rest_error
end

Instance Method Details

#to_sObject



103
104
105
# File 'lib/jamf/api/connection/api_error.rb', line 103

def to_s
  @errors.map(&:to_s).join '; '
end