Exception: AppStoreServerApi::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/app_store_server_api/error.rb

Defined Under Namespace

Classes: InvalidResponseError, InvalidTestNotificationTokenError, InvalidTransactionIdError, RateLimitExceededError, ServerError, ServerNotificationURLNotFoundError, TestNotificationNotFoundError, TransactionIdNotFoundError, UnauthorizedError

Constant Summary collapse

ERROR_CODE_MAP =

map error code to error class

{
  4040010 => Error::TransactionIdNotFoundError,
  4000020 => Error::InvalidTestNotificationTokenError,
  4000006 => Error::InvalidTransactionIdError,
  4290000 => Error::RateLimitExceededError,
  4040007 => Error::ServerNotificationURLNotFoundError,
  4040008 => Error::TestNotificationNotFoundError,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, message:, response:) ⇒ Error

initialize error

Parameters:

  • code (Integer)

    error code

  • message (String)

    error message

  • response (Faraday::Response)

    error response



11
12
13
14
15
# File 'lib/app_store_server_api/error.rb', line 11

def initialize(code:, message:, response:)
  super(message)
  @code = code
  @response = response
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/app_store_server_api/error.rb', line 5

def code
  @code
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/app_store_server_api/error.rb', line 5

def response
  @response
end

Class Method Details

.handle_error(response) ⇒ Object

raise error from response

Parameters:

  • response (Faraday::Response)

    error response



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/app_store_server_api/error.rb', line 83

def self.handle_error(response)
  case response.status
  when 401
    # Unauthorized error
    # reasons:
    # - JWT in the authorization header is invalid.
    raise Error::UnauthorizedError.new(response: response)
  when 500
    raise Error::ServerError.new(response: response)
  else
    data = JSON.parse(response.body)

    # error object must be {errorCode: Integer, errorMessage: String}
    unless data.has_key?('errorCode') && data.has_key?('errorMessage')
      raise Error::InvalidResponseError.new(message: 'response body is invalid', response: response)
    end

    error_code = data['errorCode']
    error_class = ERROR_CODE_MAP[error_code] || Error
    raise error_class.new(code: error_code, message: data['errorMessage'], response: response)
  end
end

Instance Method Details

#inspectObject



25
26
27
# File 'lib/app_store_server_api/error.rb', line 25

def inspect
  "#<#{self.class.name}: #{to_h.to_json}>"
end

#to_hObject



17
18
19
20
21
22
23
# File 'lib/app_store_server_api/error.rb', line 17

def to_h
  {
    code: code,
    message: message,
    response: response
  }
end