davixlabs-h2i 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DavixH2I
4
+ # The error codes, with their family and whether a retry makes sense, come from the API contract.
5
+ class Error < StandardError
6
+ attr_reader :code, :status, :hint, :details, :request_id, :renewal_url, :retryable, :retry_after, :body
7
+
8
+ def initialize(message, code: nil, status: nil, hint: nil, details: nil, request_id: nil, renewal_url: nil, retryable: false, retry_after: nil, body: nil)
9
+ super(message)
10
+ @code = code
11
+ @status = status
12
+ @hint = hint
13
+ @details = details
14
+ @request_id = request_id
15
+ @renewal_url = renewal_url
16
+ @retryable = retryable ? true : false
17
+ @retry_after = retry_after
18
+ @body = body
19
+ end
20
+ end
21
+
22
+ class ApiError < Error; end
23
+ class ValidationError < ApiError; end
24
+ class UploadError < ApiError; end
25
+ class AuthenticationError < ApiError; end
26
+ class PlanError < ApiError; end
27
+ class QuotaError < ApiError; end
28
+ class RateLimitError < QuotaError; end
29
+ class ProcessingError < ApiError; end
30
+ class ServerError < ApiError; end
31
+ class NotFoundError < ApiError; end
32
+ class ConflictError < ApiError; end
33
+ class AiError < ApiError; end
34
+ class KeyManagementError < ApiError; end
35
+ class JobError < ApiError; end
36
+ class SignedUrlError < ApiError; end
37
+ class NetworkError < Error; end
38
+ class TimeoutError < NetworkError; end
39
+ # The SDK was used wrongly: a bad option, a forbidden path, an invalid parameter.
40
+ class ConfigError < Error; end
41
+
42
+ BY_FAMILY = {
43
+ 'request' => ValidationError, 'upload' => UploadError, 'auth' => AuthenticationError, 'plan' => PlanError, 'quota' => QuotaError,
44
+ 'processing' => ProcessingError, 'server' => ServerError, 'ai' => AiError, 'keys' => KeyManagementError, 'jobs' => JobError,
45
+ 'signed-url' => SignedUrlError
46
+ }.freeze
47
+
48
+ def self.error_class(status, family)
49
+ return RateLimitError if status == 429
50
+ return NotFoundError if status == 404 && !BY_FAMILY.key?(family)
51
+ return ConflictError if status == 409
52
+ return BY_FAMILY[family] if BY_FAMILY.key?(family)
53
+ return ServerError if status >= 500
54
+ return AuthenticationError if [401, 403].include?(status)
55
+
56
+ ApiError
57
+ end
58
+ end