Exception: Jamf::Connection::JamfProAPIError

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

Overview

An exception class that’s a wrapper around Jamf::OAPIObject::ApiError

Constant Summary collapse

RSRC_NOT_FOUND =
'Resource Not Found'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ JamfProAPIError

Returns a new instance of JamfProAPIError.

Parameters:

  • http_response (Faraday::Response)


43
44
45
46
47
48
49
# File 'lib/jamf/api/connection/jamf_pro_api_error.rb', line 43

def initialize(http_response)
  @http_response = http_response
  @api_error = Jamf::OAPISchemas::ApiError.new @http_response.body

  add_common_basic_error_causes if @api_error.errors.empty?
  super
end

Instance Attribute Details

#api_errorJamf::OAPIObject::ApiError (readonly)

Returns:

  • (Jamf::OAPIObject::ApiError)


40
41
42
# File 'lib/jamf/api/connection/jamf_pro_api_error.rb', line 40

def api_error
  @api_error
end

#http_responseFaraday::Response (readonly)

Returns:

  • (Faraday::Response)


37
38
39
# File 'lib/jamf/api/connection/jamf_pro_api_error.rb', line 37

def http_response
  @http_response
end

Instance Method Details

#add_common_basic_error_causesObject

If no actual errors causes came with the APIError, try to add some common basic ones



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jamf/api/connection/jamf_pro_api_error.rb', line 53

def add_common_basic_error_causes
  return unless api_error.errors.empty?

  case http_response.status
  when 403
    code = 'INVALID_PRIVILEGE'
    desc = 'Forbidden'
    id = nil
    field = ''
  when 404
    code = 'NOT_FOUND'
    desc = "'#{http_response.env.url.path}' was not found on the server"
    id = nil
    field = ''
  else
    return
  end # case

  api_error.errors_append Jamf::OAPISchemas::ApiErrorCause.new(field: field, code: code, description: desc, id: id)
end

#api_statusObject

http status, from the API error



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

def api_status
  api_error.httpStatus
end

#errorsArray<Jamf::OAPIObject::ApiErrorCause>

Returns the causes of the error.

Returns:

  • (Array<Jamf::OAPIObject::ApiErrorCause>)

    the causes of the error



85
86
87
# File 'lib/jamf/api/connection/jamf_pro_api_error.rb', line 85

def errors
  api_error.errors
end

#http_statusObject

http status, from the server http response



75
76
77
# File 'lib/jamf/api/connection/jamf_pro_api_error.rb', line 75

def http_status
  http_response.status
end

#to_sObject

To string, this shows up as the exception msg when raising the exception



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jamf/api/connection/jamf_pro_api_error.rb', line 90

def to_s
  msg = +"HTTP #{http_status}"
  msg << ':' unless errors.empty?
  msg << errors.map do |err|
    err_str = +''
    err_str << " Field: #{err.field}" unless err.field.to_s.empty?
    err_str << ' Error:' if err.code || err.description
    err_str << " #{err.code}" if err.code
    err_str << " #{err.description}" if err.description
    err_str << " Object ID: '#{err.id}'" if err.id
    err_str
  end.join('; ')
  msg
end