Class: TiktokBusinessApi::ErrorFactory
- Inherits:
-
Object
- Object
- TiktokBusinessApi::ErrorFactory
- Defined in:
- lib/tiktok_business_api/errors.rb
Overview
Factory for creating the appropriate error object
Class Method Summary collapse
-
.from_response(response, request = nil) ⇒ TiktokBusinessApi::Error
Create an error object based on the response.
Class Method Details
.from_response(response, request = nil) ⇒ TiktokBusinessApi::Error
Create an error object based on the response
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 |
# File 'lib/tiktok_business_api/errors.rb', line 51 def self.from_response(response, request = nil) status = response.status body = if response.body && !response.body.empty? begin JSON.parse(response.body) rescue JSON::ParserError { error: "Invalid JSON response: #{response.body}" } end else {} end # Parse TikTok API response which has its own error structure error_code = body.is_a?(Hash) ? body['code'] : nil = body.is_a?(Hash) ? body['message'] : nil # Determine the error class based on status and error code klass = case status when 401 AuthenticationError when 403 AuthorizationError when 429 RateLimitError when 400..499 InvalidRequestError when 500..599 ApiError else Error end # Create and return the error klass.new( || "HTTP #{status}", status, body, request ) end |