Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 18, 2025

Error interceptors in the custom client were not being triggered when network-level failures occurred (CORS errors, DNS failures, connection timeouts, etc.). This meant that developers' error handling and logging code was completely bypassed for some of the most common request failure scenarios.

Problem demonstrated:

const client = createClient({
  baseUrl: 'https://invalid-domain.com', // Will cause network error
});

client.interceptors.error.use((error, response) => {
  console.log('This should be called but wasn\'t!');
  return error;
});

// Before fix: interceptor never called for network errors
await client.get({ url: '/test' });

Root cause:
The fetch() call was not wrapped in a try-catch block, so network exceptions bypassed the error interceptor logic entirely. Only HTTP response errors (4xx, 5xx status codes) were being handled.

Solution:

  • Wrapped the fetch call in try-catch to capture network-level errors
  • Updated the ErrInterceptor type signature to accept response: Res | undefined since network errors don't have a response object
  • When network errors occur, error interceptors are called with undefined as the response parameter
  • Maintained full backward compatibility for existing HTTP response error handling

After fix:

// Network errors now properly trigger interceptors
client.interceptors.error.use((error, response) => {
  if (response) {
    console.log('HTTP error:', response.status);
  } else {
    console.log('Network error:', error.message); // Now called!
  }
  return error;
});

This makes error interceptors a reliable tool for handling all types of request failures, improving debugging capabilities and making the client behavior more predictable.

Fixes #1994.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • messedupbaseurl
    • Triggering command: node /tmp/demo-fix-1994.js (dns block)
  • nonexistent-domain-12345.com
    • Triggering command: node /tmp/test-error-interceptors.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link

vercel bot commented Sep 18, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
hey-api-docs Ready Ready Preview Comment Sep 18, 2025 11:44am

Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

changeset-bot bot commented Sep 18, 2025

⚠️ No Changeset found

Latest commit: 1776d8f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

- Wrap fetch call in try-catch to handle network-level errors (CORS, DNS, etc.)
- Update error interceptor type signature to allow undefined response
- Add comprehensive tests for both network and response errors
- Maintain backward compatibility for existing error handling

Co-authored-by: mrlubos <[email protected]>
@Copilot Copilot AI changed the title [WIP] Client interceptor not working for failed requests? Fix error interceptors not being called for network failures Sep 18, 2025
@Copilot Copilot AI requested a review from mrlubos September 18, 2025 11:46
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.

Client interceptor not working for failed requests?
2 participants