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 status 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



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

def initialize(message = nil, response = nil)
  @response = response
  @status = if response.nil?
            NOCODE
          else
            response[:status]
          end

  begin
    json = JSON.parse(response[:body])
    api_message = json['message'] || json['Error'] || json['error']
  rescue JSON::ParserError
    api_message = response.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
  @status = NOCODE
  super(message, response)
end

Instance Attribute Details

#statusObject (readonly)

Public: gets HTTP status status



35
36
37
# File 'lib/mailgun/exceptions/exceptions.rb', line 35

def status
  @status
end