-
Notifications
You must be signed in to change notification settings - Fork 72
/
error.rb
83 lines (68 loc) · 2.15 KB
/
error.rb
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
42
43
44
45
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
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true
# Copyright (C) 2019 Twitter, Inc.
module TwitterAds
class Error < StandardError
attr_reader :code, :headers, :response, :details
def initialize(*args)
if args.size == 1 && args[0].respond_to?(:body) && args[0].respond_to?(:code)
@response = args[0]
@code = args[0].code
@details = args[0].body[:errors] if args[0].body.is_a?(Hash) && args[0].body[:errors]
elsif args.size == 3
@response = args[0]
@details = args[1]
@code = args[2]
end
self
end
def inspect
str = +"#<#{self.class.name}:0x#{object_id}"
str << " code=#{@code}" if @code
str << " details=\"#{@details}\"" if @details
str << '>'
end
alias to_s inspect
class << self
ERRORS = {
400 => 'TwitterAds::BadRequest',
401 => 'TwitterAds::NotAuthorized',
403 => 'TwitterAds::Forbidden',
404 => 'TwitterAds::NotFound',
429 => 'TwitterAds::RateLimit',
500 => 'TwitterAds::ServerError',
503 => 'TwitterAds::ServiceUnavailable'
}.freeze
# Returns an appropriately typed Error object based from an API response.
#
# @param object [Hash] The parsed JSON API response.
#
# @return [Error] The error object instance.
#
# @since 0.1.0
# @api private
def from_response(object)
return class_eval(ERRORS[object.code]).new(object) if ERRORS.key?(object.code)
new(object) # fallback, unknown error
end
end
end
# Server Errors (5XX)
class ServerError < Error; end
class ServiceUnavailable < ServerError; end
# Client Errors (4XX)
class ClientError < Error; end
class NotAuthorized < ClientError; end
class Forbidden < ClientError; end
class NotFound < ClientError; end
class BadRequest < ClientError; end
class RateLimit < ClientError
attr_reader :reset_at
def initialize(object)
super object
header = object.headers.fetch('x-account-rate-limit-reset', nil) ||
object.headers.fetch('x-rate-limit-reset', nil)
@reset_at = header.first.to_i
self
end
end
end