Exception: Alula::AlulaError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/alula/errors.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ AlulaError



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/alula/errors.rb', line 4

def initialize(error)
  if error.class == String
    @message = error
    return
  end

  @http_status = error.http_status
  @raw_response = error
  @error = error.data['error']
  @message = error.data['error_description'] || error.data['message']
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



3
4
5
# File 'lib/alula/errors.rb', line 3

def error
  @error
end

#http_statusObject (readonly)

Returns the value of attribute http_status.



3
4
5
# File 'lib/alula/errors.rb', line 3

def http_status
  @http_status
end

#messageObject (readonly)

Returns the value of attribute message.



3
4
5
# File 'lib/alula/errors.rb', line 3

def message
  @message
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



3
4
5
# File 'lib/alula/errors.rb', line 3

def raw_response
  @raw_response
end

Class Method Details

.critical_error_for_response(error_text) ⇒ Object

Handle HTML-based errors from the API



44
45
46
# File 'lib/alula/errors.rb', line 44

def self.critical_error_for_response(error_text)
  InvalidRequestError.new(error_text.first.first)
end

.error_for_response(response) ⇒ Object

Figure out what error should be raised



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/alula/errors.rb', line 49

def self.error_for_response(response)
  case response.data['error']
  when 'RateLimit'
    Alula.logger.error response
    RateLimitError.new(response)
  when 'invalid_token'
    InvalidTokenError.new(response)
  when 'insufficient_scope'
    InsufficientScopeError.new(response)
  when 'server_error'
    Alula.logger.error response
    ServerError.new(response)
  else
    #
    # RPC errors are identified by jsonrpc in the body.
    return ProcedureError.new(response) if response.data['jsonrpc']


    Alula.logger.error response
    raise NotImplementedError.new("Unable to derive error for #{response.data['error']}")
  end
end

.errors_for_response(response) ⇒ Object

Some responses have an error array. We won’t be able to raise on all errors but we can raise on the first one



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/alula/errors.rb', line 73

def self.errors_for_response(response)
  error = response.data['errors'].first
  case error['title']
  when 'RateLimit'
    RateLimitError.new(response)
  when 'Forbidden'
    ForbiddenError.new(error['detail'])
  when 'Not Found'
    NotFoundError.new(error['detail'])
  when 'Bad Request'
    Alula.logger.error response
    BadRequestError.new(error['detail'])
  when 'API Error Unknown'
    Alula.logger.error response
    UnknownError.new(error['detail'])
  when 'Insufficient Scope'
    InsufficientScopeError.new(error['detail'])
  else
    #
    # RPC errors are identified by jsonrpc in the body.
    return ProcedureError.new(response) if response.data['jsonrpc']

    Alula.logger.error response
    raise NotImplementedError.new("Unable to derive error for #{response.data['errors']}")
  end
end

.for_response(response) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/alula/errors.rb', line 20

def self.for_response(response)
  if !response.data['error'].nil? && !response.data['error'].empty?
    self.error_for_response(response)

  elsif !response.data['errors'].nil? && !response.data['errors'].empty?
    self.errors_for_response(response)

  elsif response.data.match(/^<!DOCTYPE html>/)
    self.critical_error_for_response(response.data.scan(/<pre>(.*)<\/pre>/))
  else
    message = "Unable to derive error from response: #{response.inspect}"
    Alula.logger.error message
    raise UnknownApiError.new(message)
  end

rescue NoMethodError
  message = "Unable to derive error from response: #{response.inspect}"
  Alula.logger.error message
  raise UnknownApiError.new(message)
end

Instance Method Details

#ok?Boolean



16
17
18
# File 'lib/alula/errors.rb', line 16

def ok?
  false
end