Class: BlockScore::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/blockscore/error/error_handler.rb

Instance Method Summary collapse

Constructor Details

#initializeErrorHandler

Returns a new instance of ErrorHandler.



5
6
# File 'lib/blockscore/error/error_handler.rb', line 5

def initialize()
end

Instance Method Details

#check_error(response) ⇒ Object

Function: check_error()

response -



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/blockscore/error/error_handler.rb', line 13

def check_error(response)

  @code = response.code
  @type = response.headers['content-type']

  if (200 <= @code and @code < 300)
    return response
  end

  @body = get_body(response)
  @message = get_message(@body)
  @error_type = @error_code = @param = nil

  # Internal API Error
  if @code == 500
    raise BlockScore::InternalServerError.new(@message, @body, @error_type)
  end

  if @body.include? 'error'
    @error = @body['error']
    @error_type = get_value(@error, 'type')
    @error_code = get_value(@error, 'code')
    @param = get_value(@error, 'param')
  end # body.include? 'error'

  process_code(@code)

end

#get_body(response) ⇒ Object

Function: get_body()

response -



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/blockscore/error/error_handler.rb', line 87

def get_body(response)
  type = response.headers['content-type']
  body = response.body

  # If response body is in JSON
  if type.include? 'json'
    body = JSON.parse(body)
  end # type.include?

  body
  
end

#get_message(body) ⇒ Object

Function: get_message()

body -



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/blockscore/error/error_handler.rb', line 106

def get_message(body)
  message = ''
  if body.is_a? String
    message = body
  
  elsif body.is_a? Hash
    if body.include? 'error'
      message = body['error']['message']
    else
      message = 'Unable to select error message from json returned by request responsible for error'
    end # if body.include?
  else
    message = 'Unable to understand the content type of response returned by request responsible for error'
  end # if body.is_a? String

  message
end

#get_value(obj, key) ⇒ Object

Function: get_value()

obj - key -



131
132
133
134
135
136
137
138
# File 'lib/blockscore/error/error_handler.rb', line 131

def get_value(obj, key)

  if obj.include? key
    return obj[key]
  end

  nil
end

#process_code(code) ⇒ Object

Function: process_code()

Tries to determine which error to raise.

code -



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/blockscore/error/error_handler.rb', line 50

def process_code(code)

  # Input data error
  if code == 400
    # Could not be validated.
    # Which type of input error?
    if @param
      raise BlockScore::ValidationError.new(@message, @body, @error_type, @param, @error_code)
    
    # Required parameter is missing
    else
      raise BlockScore::ParameterError.new(@message, @body, @error_type)
    end # if param
  
  # Error with an API Key
  elsif code == 401
    raise BlockScore::AuthorizationError.new(@message, @body, @error_type)
  
  # Trying to access nonexistent endpoint
  elsif code == 404
    raise BlockScore::NotFoundError.new(@message, @body, @error_type)

  # Generic BlockscoreError (fallback)
  else
    raise BlockScore::BlockscoreError.new(@message, @body)

  end # end code checking

end