~tim/mailchimp3

mailchimp3/lib/mailchimp3/errors.rb -rw-r--r-- 1.3 KiB
836bcd45Tim Morgan Release 1.3.1 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module MailChimp3
  module Errors
    class AuthRequiredError < StandardError; end
    class DataCenterRequiredError < StandardError; end

    class BaseError < StandardError
      attr_reader :status, :details

      def initialize(body:, status:)
        @status = status
        @message = if body.is_a?(Hash) && body['error']
                     "#{body['name']}: #{body['error']}"
                   elsif body.is_a?(Hash)
                     "#{body['title']}: #{body['detail']}"
                   else
                     body.to_s
                   end
        @details = body if body.is_a?(Hash)
      end

      def to_s
        @message
      end

      def inspect
        "<#{self.class.name} status=#{@status} message=#{@message}>"
      end
    end

    class ClientError              < BaseError;   end # 400..499
    class BadRequest               < ClientError; end # 400
    class Unauthorized             < ClientError; end # 401
    class Forbidden                < ClientError; end # 403
    class NotFound                 < ClientError; end # 404
    class MethodNotAllowed         < ClientError; end # 405
    class UnprocessableEntity      < ClientError; end # 422

    class ServerError              < BaseError;   end # 500..599
    class InternalServerError      < ServerError; end # 500
  end
end