Timeout and network issues

Last updated: 3 months ago

Network problems can cause API requests to fail. Here's how to handle timeouts and connection issues gracefully.

Setting appropriate timeouts

Always set timeouts to avoid hanging requests:

// JavaScript with fetch
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 30000); // 30s
try {
  const response = await fetch('https://api.cathopic.com/resources', {
    method: 'POST',
    headers: {
      'x-api-key': apiKey,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ query: 'Jesus' }),
    signal: controller.signal
  });
  
  clearTimeout(timeout);
  return await response.json();
  
} catch (error) {
  clearTimeout(timeout);
  
  if (error.name === 'AbortError') {
    console.error('Request timed out after 30s');
  } else {
    console.error('Network error:', error);
  }
  throw error;
}
// Python with requests
import requests
from requests.exceptions import Timeout, RequestException
try:
    response = requests.post(
        'https://api.cathopic.com/resources',
        headers={'x-api-key': api_key},
        json={'query': 'Jesus'},
        timeout=30  # 30 seconds
    )
    return response.json()
    
except Timeout:
    print('Request timed out after 30s')
    raise
except RequestException as e:
    print(f'Network error: {e}')
    raise

Common network errors

Connection timeout:

  • Can't establish connection to the server

  • Usually network or firewall issue

  • Check internet connection

  • Verify firewall allows HTTPS to api.cathopic.com

Read timeout:

  • Connection established but no response

  • Server may be slow or overloaded

  • Increase timeout or retry later

DNS resolution failure:

  • Can't resolve api.cathopic.com

  • DNS server issue

  • Try again or check DNS settings

Retry with backoff

async function fetchWithRetry(url, options, maxRetries = 3) {
  for (let attempt = 0; attempt < maxRetries; attempt++) {
    try {
      const controller = new AbortController();
      const timeout = setTimeout(() => controller.abort(), 30000);
      
      const response = await fetch(url, {
        ...options,
        signal: controller.signal
      });
      
      clearTimeout(timeout);
      
      if (!response.ok) {
        throw new Error(`HTTP ${response.status}`);
      }
      
      return await response.json();
      
    } catch (error) {
      const isLastAttempt = attempt === maxRetries - 1;
      
      if (isLastAttempt) {
        console.error(`All ${maxRetries} attempts failed`);
        throw error;
      }
      
      const waitTime = Math.pow(2, attempt) * 1000;
      console.log(`Attempt ${attempt + 1} failed. Retrying in ${waitTime}ms...`);
      
      await new Promise(resolve => setTimeout(resolve, waitTime));
    }
  }
}

Recommended timeout values

  • Connection timeout: 10 seconds

  • Read timeout: 30 seconds

  • /resources search: 30 seconds

  • /confirm licensing: 30 seconds

Debugging network issues

Use curl to test connectivity:

# Test basic connectivity
curl -v https://api.cathopic.com/resources
# Test with timeout
curl --max-time 30 -X POST https://api.cathopic.com/resources \
  -H "x-api-key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query": "test"}'

Check for:

  • ✓ DNS resolves api.cathopic.com

  • ✓ SSL/TLS connection succeeds

  • ✓ HTTP response received

  • ✓ Response time reasonable (<5s typically)

If issues persist, contact support with network diagnostic details.

Was this article helpful?