-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.rb
99 lines (71 loc) · 2.94 KB
/
errors.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true
module Increase
class Error < StandardError; end
class ApiError < Error
attr_reader :response
attr_reader :detail
attr_reader :status
attr_reader :title
attr_reader :type
def initialize(message, response: nil, detail: nil, status: nil, title: nil, type: nil)
@response = response
@detail = detail || response.body["detail"]
@status = status || response.body["status"]
@title = title || response.body["title"]
@type = type || response.body["type"]
super(message)
end
class << self
def from_response(response)
type = response.body["type"]
klass = ERROR_TYPES[type]
# Fallback in case of really bad 5xx error
klass ||= InternalServerError if (500..599).cover?(response.status)
# Handle case of an unknown error
klass ||= ApiError
code = [response.body["status"] || response.status, type].compact.join(": ") || "Error"
message = [response.body["title"], response.body["detail"]].compact.join(" ") || "Increase API Error"
klass.new("[#{code}] #{message}", response: response)
end
end
end
class ApiMethodNotFoundError < ApiError; end
class EnvironmentMismatchError < ApiError; end
class IdempotencyConflictError < ApiError; end
class IdempotencyUnprocessableError < ApiError; end
class InsufficientPermissionsError < ApiError; end
class InternalServerError < ApiError; end
class InvalidApiKeyError < ApiError; end
class InvalidOperationError < ApiError; end
class InvalidParametersError < ApiError; end
class MalformedRequestError < ApiError; end
class ObjectNotFoundError < ApiError; end
class PrivateFeatureError < ApiError; end
class RateLimitedError < ApiError; end
ERROR_TYPES = {
"api_method_not_found_error" => ApiMethodNotFoundError,
"environment_mismatch_error" => EnvironmentMismatchError,
"idempotency_conflict_error" => IdempotencyConflictError,
"idempotency_unprocessable_error" => IdempotencyUnprocessableError,
"insufficient_permissions_error" => InsufficientPermissionsError,
"internal_server_error" => InternalServerError,
"invalid_api_key_error" => InvalidApiKeyError,
"invalid_operation_error" => InvalidOperationError,
"invalid_parameters_error" => InvalidParametersError,
"malformed_request_error" => MalformedRequestError,
"object_not_found_error" => ObjectNotFoundError,
"private_feature_error" => PrivateFeatureError,
"rate_limited_error" => RateLimitedError
}
# WebhookSignatureVerificationError is raised when a received webhook's
# signature is invalid.
class WebhookSignatureVerificationError < Error
attr_reader :signature_header
attr_reader :payload
def initialize(message = "Increase webhook signature verification failed", signature_header: nil, payload: nil)
@signature_header = signature_header
@payload = payload
super(message)
end
end
end