Exception: Mailgun::CommunicationError

Inherits:
Error
  • Object
show all
Defined in:
lib/mailgun/exceptions/exceptions.rb

Overview

Public: Class for managing communications (eg http) response errors Inherits from Mailgun::Error

Direct Known Subclasses

BadRequest, Unauthorized

Constant Summary collapse

NOCODE =

Public: fallback if there is no response code on the object

000
FORBIDDEN =
'Forbidden'

Instance Attribute Summary collapse

Attributes inherited from Error

#object

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, response = nil) ⇒ CommunicationError

Public: initialization of new error given a message and/or object

message - a String detailing the error response - a RestClient::Response object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mailgun/exceptions/exceptions.rb', line 44

def initialize(message = nil, response = nil)
  @response = response
  @code = if response.nil?
            NOCODE
          else
            response.code
          end

  begin
    api_message = JSON.parse(response.body)['message']
  rescue JSON::ParserError
    api_message = response.body
  rescue NoMethodError
    api_message = "Unknown API error"
  rescue
    api_message = 'Unknown API error'
  end

  message = message || ''
  message = message + ': ' + api_message

  super(message, response)
rescue NoMethodError, JSON::ParserError
  @code = NOCODE
  super(message, response)
end

Instance Attribute Details

#codeObject (readonly)

Public: gets HTTP status code



33
34
35
# File 'lib/mailgun/exceptions/exceptions.rb', line 33

def code
  @code
end