Exception: WOTC::Error

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

Overview

Custom error class for rescuing from all wotc.com errors

Direct Known Subclasses

ClientError, ServerError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Error

Returns a new instance of Error.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/wotc/error.rb', line 6

def initialize(response)
  @response = response.dup
  env = response.env
  # Use hash-style access for :method to avoid calling Kernel#method.
  # Plain Ruby only: this gem does not depend on ActiveSupport.
  http_method = env[:method].to_s.upcase
  @http_method = http_method.empty? ? "UNKNOWN" : http_method
  @url = env.url.to_s
  @status = response.status
  @body = response.body
  if @body.is_a?(Hash) && !@body.empty? && !@body.fetch("errors", nil).nil?
    @raw_errors = @body.fetch("errors")
  end
  super()
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/wotc/error.rb', line 4

def errors
  @errors
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



4
5
6
# File 'lib/wotc/error.rb', line 4

def http_method
  @http_method
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/wotc/error.rb', line 4

def url
  @url
end

Instance Method Details

#error_sentenceObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/wotc/error.rb', line 35

def error_sentence
  return if @raw_errors.nil?

  array = []
  @raw_errors.each do |_, v| 
    array += v
  end

  array.join(' ')
end

#messageObject



22
23
24
25
26
27
28
29
# File 'lib/wotc/error.rb', line 22

def message
  <<-HEREDOC
  URL: #{@url}
  method: #{@http_method}
  response status: #{@status}
  response body: #{@body}
  HEREDOC
end

#raw_errorsObject



31
32
33
# File 'lib/wotc/error.rb', line 31

def raw_errors
  @raw_errors
end