Exception: MailchimpAPI::Errors::FailedRequest

Inherits:
MailchimpAPI::Error show all
Defined in:
lib/mailchimp-api/error.rb

Overview

Raised when Mailchimp responds with any status code except 200, 201, 202, 204

Instance Attribute Summary

Attributes inherited from MailchimpAPI::Error

#error_detail, #error_fields, #error_instance, #error_status, #error_title, #error_type, #request, #response

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, request:, response:) ⇒ FailedRequest

Initializes a new FailedRequest error

Examples:

begin
  client.get("/lists/123")
rescue MailchimpAPI::Errors::FailedRequest => e
  puts e.error_title # => "Resource Not Found"
  puts e.error_detail # => "The requested resource could not be found."
end

Parameters:

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

    Error message

  • request (Request)

    The request that failed

  • response (Response)

    The response received from Mailchimp



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mailchimp-api/error.rb', line 46

def initialize(message = nil, request:, response:)
  @request = request
  @response = response

  body = response.body
  data = body.is_a?(Hash) ? body : {}
  @error_type = data[:type]
  @error_title = data[:title] || response.http_response.class.name
  @error_detail = data[:detail]
  @error_status = data[:status] || response.http_status
  @error_fields = data[:errors]
  @error_instance = data[:instance]

  message += "\n  #{response.http_body}" unless data.empty?
  super(message)
end