Class: TiktokBusinessApi::ErrorFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/tiktok_business_api/errors.rb

Overview

Factory for creating the appropriate error object

Class Method Summary collapse

Class Method Details

.from_response(response, request = nil) ⇒ TiktokBusinessApi::Error

Create an error object based on the response

Parameters:

  • response (Faraday::Response)

    The HTTP response

  • request (Hash) (defaults to: nil)

    The request that caused the error

Returns:



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
# File 'lib/tiktok_business_api/errors.rb', line 51

def self.from_response(response, request = nil)
  status = response.status
  body = response.body
  
  # Parse TikTok API response which has its own error structure
  error_code = body.is_a?(Hash) ? body['code'] : nil
  error_message = 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(
    error_message || "HTTP #{status}",
    status,
    body,
    request
  )
end