Exception: VerifyAnyEmail::Error

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

Overview

Raised for any non-success API response, transport failure, or invalid usage.

When the API returns a JSON error body of the shape { "error": "<code>", "message": "<human message>" }, the #code and #message readers expose those fields and #status carries the HTTP status code. Notable statuses:

401 - Missing or invalid API key
402 - Insufficient credits
413 - Batch too large for your plan
429 - Rate limited / concurrent batch limit reached

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, status: nil, code: nil, body: nil) ⇒ Error

Returns a new instance of Error.

Parameters:

  • message (String) (defaults to: nil)

    Human-readable message.

  • status (Integer, nil) (defaults to: nil)

    HTTP status code.

  • code (String, nil) (defaults to: nil)

    Machine-readable error code.

  • body (Hash, nil) (defaults to: nil)

    Parsed JSON body.



29
30
31
32
33
34
# File 'lib/verifyanyemail/error.rb', line 29

def initialize(message = nil, status: nil, code: nil, body: nil)
  @status = status
  @code = code
  @body = body
  super(message || code || "VerifyAnyEmail::Error")
end

Instance Attribute Details

#bodyHash? (readonly)

Returns The raw parsed JSON body of the error response, if any.

Returns:

  • (Hash, nil)

    The raw parsed JSON body of the error response, if any.



23
24
25
# File 'lib/verifyanyemail/error.rb', line 23

def body
  @body
end

#codeString? (readonly)

Returns Machine-readable error code from the JSON body (error).

Returns:

  • (String, nil)

    Machine-readable error code from the JSON body (error).



20
21
22
# File 'lib/verifyanyemail/error.rb', line 20

def code
  @code
end

#statusInteger? (readonly)

Returns HTTP status code, when the error came from a response.

Returns:

  • (Integer, nil)

    HTTP status code, when the error came from a response.



17
18
19
# File 'lib/verifyanyemail/error.rb', line 17

def status
  @status
end

Class Method Details

.from_response(status, parsed, raw_body = nil) ⇒ VerifyAnyEmail::Error

Build an VerifyAnyEmail::Error from an HTTP status and a parsed (or unparsed) body.

Parameters:

  • status (Integer)
  • parsed (Hash, nil)

    Parsed JSON body (expects error/message keys).

  • raw_body (String, nil) (defaults to: nil)

    Raw response body, used as a fallback message.

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/verifyanyemail/error.rb', line 60

def self.from_response(status, parsed, raw_body = nil)
  code = nil
  message = nil

  if parsed.is_a?(Hash)
    code = parsed["error"] || parsed[:error]
    message = parsed["message"] || parsed[:message]
  end

  message ||= raw_body
  message = "HTTP #{status}" if message.nil? || message.to_s.empty?

  new(message, status: status, code: code, body: (parsed.is_a?(Hash) ? parsed : nil))
end

Instance Method Details

#payment_required?Boolean

True when the account has insufficient credits (HTTP 402).

Returns:

  • (Boolean)


44
45
46
# File 'lib/verifyanyemail/error.rb', line 44

def payment_required?
  status == 402
end

#rate_limited?Boolean

True when the request was rate limited (HTTP 429).

Returns:

  • (Boolean)


50
51
52
# File 'lib/verifyanyemail/error.rb', line 50

def rate_limited?
  status == 429
end

#unauthorized?Boolean

True when the failure is due to a missing or invalid API key (HTTP 401).

Returns:

  • (Boolean)


38
39
40
# File 'lib/verifyanyemail/error.rb', line 38

def unauthorized?
  status == 401
end