mirror of
https://github.com/we-promise/sure.git
synced 2026-05-12 23:25:00 +00:00
Add rate limit error handling for TwelveData provider
- Add RateLimitError class to Provider::TwelveData - Implement custom error transformer to detect 429 errors - Re-raise rate limit errors in Security::Price::Importer - Configure SyncJob to retry on rate limit errors with 70s initial delay Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>
This commit is contained in:
@@ -5,6 +5,7 @@ class Provider::TwelveData < Provider
|
||||
Error = Class.new(Provider::Error)
|
||||
InvalidExchangeRateError = Class.new(Error)
|
||||
InvalidSecurityPriceError = Class.new(Error)
|
||||
RateLimitError = Class.new(Error)
|
||||
|
||||
def initialize(api_key)
|
||||
@api_key = api_key
|
||||
@@ -231,4 +232,40 @@ class Provider::TwelveData < Provider
|
||||
faraday.headers["Authorization"] = "apikey #{api_key}"
|
||||
end
|
||||
end
|
||||
|
||||
# Custom error transformer to detect rate limiting errors
|
||||
def default_error_transformer(error)
|
||||
if error.is_a?(Faraday::Error)
|
||||
response_body = error.response&.dig(:body)
|
||||
status_code = error.response&.dig(:status)
|
||||
|
||||
# Detect 429 rate limit errors
|
||||
if status_code == 429
|
||||
message = extract_error_message(response_body) || error.message
|
||||
raise RateLimitError.new(
|
||||
"TwelveData rate limit exceeded: #{message}",
|
||||
details: response_body
|
||||
)
|
||||
end
|
||||
|
||||
self.class::Error.new(
|
||||
error.message,
|
||||
details: response_body
|
||||
)
|
||||
else
|
||||
self.class::Error.new(error.message)
|
||||
end
|
||||
end
|
||||
|
||||
# Extract error message from TwelveData API response
|
||||
def extract_error_message(response_body)
|
||||
return nil unless response_body.is_a?(String)
|
||||
|
||||
begin
|
||||
parsed = JSON.parse(response_body)
|
||||
parsed.dig("message") || parsed.dig("error")
|
||||
rescue JSON::ParserError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user