Exception: Mautic::RequestError

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, message = nil) ⇒ RequestError

Returns a new instance of RequestError.



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/mautic.rb', line 16

def initialize(response, message = nil)
  @errors ||= []
  @response = response
  @request_url = response.response&.env&.url
  body = if response.body.start_with? "<!DOCTYPE html>"
           response.body.split("\n").last
         else
           response.body
         end

  json_body = begin
                JSON.parse(body)
              rescue JSON::ParserError
                { "errors" => [{ "code" => response.status, "message" => body }] }
              end
  message ||= Array(json_body['errors']).collect do |error|
    msg = error['code'].to_s
    msg << " (#{error['type']}):" if error['type']
    msg << " #{error['message']}"
    @errors << error['message']
    msg
  end.join(', ')

  super("#{@request_url} => #{message}")
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



14
15
16
# File 'lib/mautic.rb', line 14

def errors
  @errors
end

#request_urlObject (readonly)

Returns the value of attribute request_url.



14
15
16
# File 'lib/mautic.rb', line 14

def request_url
  @request_url
end

#responseObject (readonly)

Returns the value of attribute response.



14
15
16
# File 'lib/mautic.rb', line 14

def response
  @response
end