Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 1, 2025

Fixes #1105 by adding RestException to the main package exports, allowing developers to import it directly from 'twilio' instead of using deep imports, and provides comprehensive examples and documentation.

Problem

Previously, developers had to use deep imports to access RestException:

// Old approach - problematic with modern TypeScript configs
import RestException from 'twilio/lib/base/RestException'

This caused issues with modern TypeScript configurations and wasn't intuitive for developers who expected to import it from the main package like other Twilio components.

Solution

Added RestException to the main package exports following the same pattern used by other components like RequestClient, and provided comprehensive examples showing how to use it effectively.

// Now supported - clean and intuitive
import { RestException } from 'twilio'

// Also works with CommonJS
const { RestException } = require('twilio')

Changes

  • Added RestException import to src/index.ts
  • Added both type and value exports to the TwilioSDK namespace
  • Updated examples/example.js to demonstrate proper RestException handling in both callback and promise patterns
  • Added comprehensive usage examples in README.md for both CommonJS and ESM with proper error handling patterns
  • Maintains full TypeScript support with proper type definitions

Usage Examples

Error Handling with ESM:

import twilio, { RestException } from 'twilio';

try {
  const message = await client.messages.create({...});
} catch (error) {
  if (error instanceof RestException) {
    console.log(`Twilio Error ${error.code}: ${error.message}`);
    console.log(`Status: ${error.status}`);
    console.log(`More info: ${error.moreInfo}`);
  }
}

Error Handling with CommonJS:

const { RestException } = require('twilio');

client.messages.create({...})
  .catch((error) => {
    if (error instanceof RestException) {
      console.log(`Twilio Error ${error.code}: ${error.message}`);
    }
  });

Validation

  • All existing tests pass
  • TypeScript compilation succeeds
  • No breaking changes to existing functionality
  • Follows established export patterns in the codebase
  • Examples demonstrate real-world usage patterns

This change provides a significantly better developer experience with proper documentation and examples while maintaining backward compatibility and following the library's existing conventions.


💡 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.

@Copilot Copilot AI changed the title [WIP] @twilio/twilio-node/issues/1105 Raise a PR to introduce this. Add RestException export to main twilio package Aug 1, 2025
@Copilot Copilot AI requested a review from manisha1997 August 1, 2025 12:33
Copilot finished work on behalf of manisha1997 August 1, 2025 12:33
Copy link

sonarqubecloud bot commented Aug 1, 2025

@tiwarishubham635 tiwarishubham635 changed the title Add RestException export to main twilio package feat: Add RestException export to main twilio package Sep 3, 2025
@tiwarishubham635
Copy link
Contributor

@copilot update examples/example.js to catch rest exception. Also add the usage examples in readme for both commonjs and esm.

Copy link
Contributor Author

Copilot AI commented Sep 3, 2025

@copilot update examples/example.js to catch rest exception. Also add the usage examples in readme for both commonjs and esm.

Done! I've updated examples/example.js to demonstrate RestException handling and added comprehensive usage examples in the README for both CommonJS and ESM. The examples show proper error handling patterns with instanceof RestException checks and accessing error properties like code, message, status, and moreInfo. Changes committed in 22e6269.

@Copilot Copilot AI changed the title feat: Add RestException export to main twilio package feat: Add RestException export to main twilio package with examples and documentation Sep 3, 2025
@Copilot Copilot AI requested a review from tiwarishubham635 September 3, 2025 07:53
Copilot finished work on behalf of tiwarishubham635 September 3, 2025 07:53
@tiwarishubham635 tiwarishubham635 marked this pull request as ready for review September 3, 2025 09:21
Copy link
Contributor

@tiwarishubham635 tiwarishubham635 left a comment

Choose a reason for hiding this comment

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

Tested the example and readme snippets. Working fine. LGTM!

Copy link

sonarqubecloud bot commented Sep 4, 2025

@tiwarishubham635 tiwarishubham635 merged commit 8460906 into main Sep 4, 2025
11 checks passed
@tiwarishubham635 tiwarishubham635 deleted the copilot/fix-046d23c7-d048-4b86-a269-e5801547dc90 branch September 4, 2025 08:57
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.

[Feature Request]: Add root RestException export

3 participants