Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Sep 12, 2025

Previously, all Faraday network errors (DNS failures, connection timeouts, SSL errors) were re-raised as generic Twilio::REST::TwilioError, making it impossible for consumers to distinguish between retryable network errors and non-retryable API errors.

This change introduces a new Twilio::REST::NetworkError class that inherits from TwilioError and is raised specifically for network-level failures. This allows consumers to implement intelligent retry logic:

begin
  client.messages.create(to: "+1234567890", from: "+0987654321", body: "Hello!")
rescue Twilio::REST::NetworkError => e
  # Retry network errors (DNS failures, timeouts, connection issues)
  retry_with_exponential_backoff
rescue Twilio::REST::RestError => e  
  # Don't retry API errors (401, 400, 403, etc.)
  handle_api_error(e)
end

Changes Made

  • Added NetworkError class in lib/twilio-ruby/framework/rest/error.rb
  • Updated HTTP client in lib/twilio-ruby/http/http_client.rb to raise NetworkError for Faraday::Error exceptions
  • Updated tests to verify the new error distinction functionality

Error Classification

  • NetworkError (should retry): DNS resolution failures, connection timeouts, network unreachable
  • RestError (should not retry): HTTP 4xx/5xx responses from Twilio API
  • TwilioError (base class): Both inherit from this for full backward compatibility

Backward Compatibility

Fully maintained - existing code catching TwilioError continues to work unchanged since NetworkError inherits from TwilioError.

Fixes #689.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…twork and internal errors

Co-authored-by: tiwarishubham635 <[email protected]>
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@Copilot Copilot AI changed the title [WIP] Distinguish between internal errors and network errors. Add NetworkError to distinguish between network and API errors Sep 12, 2025
Copilot finished work on behalf of tiwarishubham635 September 12, 2025 12:41
@tiwarishubham635 tiwarishubham635 changed the title Add NetworkError to distinguish between network and API errors feat: Add NetworkError to distinguish between network and API errors Sep 15, 2025
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Distinguish between internal errors and network errors.
2 participants