Sha256: a0d4636958e81c41a3923e48df7d54d0ff0c7c23e6927246543662cc31a692c5

Contents?: true

Size: 1.79 KB

Versions: 13

Compression:

Stored size: 1.79 KB

Contents

module Mailgun

  # Public: A basic class for mananging errors.
  # Inherits from StandardError (previously RuntimeError) as not all errors are
  # runtime errors.
  class Error < StandardError

    # Public: get an object an error is instantiated with
    attr_reader :object

    # Public: initialize a Mailgun:Error object
    #
    # message - a String describing the error
    # object  - an object with details about the error
    def initialize(message = nil, object = nil)
      super(message)
      @object = object
    end
  end

  # Public: Class for managing parameter errors, with a pretty name.
  # Inherits from Mailgun::Error
  class ParameterError < Error; end

  # Public: Class for managing parsing errors, with a pretty name.
  # Inherits from Mailgun::Error
  class ParseError < Error; end

  # Public: Class for managing communications (eg http) response errors
  # Inherits from Mailgun::Error
  class CommunicationError < Error
    # Public: gets HTTP status code
    attr_reader :code

    # Public: fallback if there is no response code on the object
    NOCODE = 000

    # Public: initialization of new error given a message and/or object
    #
    # message  - a String detailing the error
    # response - a RestClient::Response object
    #
    def initialize(message = nil, response = nil)
      @response = response
      @code = response.code || NOCODE

      begin
        api_message = JSON.parse(response.body)['message']
      rescue JSON::ParserError
        api_message = response.body
      rescue NoMethodError
        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

  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
mailgun-ruby-1.2.4 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.2.3 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.2.0 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.1.11 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.1.10 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.1.9 lib/mailgun/exceptions/exceptions.rb
wj-mailgun-ruby-1.1.7 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.1.8 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.1.6 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.1.5 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.1.4 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.1.3 lib/mailgun/exceptions/exceptions.rb
mailgun-ruby-1.1.2 lib/mailgun/exceptions/exceptions.rb