Kind: Class
Source: packages/core/helpers/external-proxy.ts
Part of: Core
ExternalErrorProxy creates a proxy around external dependencies so errors thrown by third-party clients can be handled consistently. It centralizes external error interception and translation, allowing the rest of the application to interact with integrations through a safer, uniform interface.
Methods
| Method | Signature | Returns |
|---|---|---|
createProxy | createProxy(targetCallback: (...args: any[]) => any, exceptionsHandler: ExternalExceptionsHandler, type: TContext) | void |
When something fails
ExternalErrorProxyhandles failure in 1 place: it turns it into a return value in all 1.
Diagram
mermaidgraph LR A[Application Service] --> B[ExternalErrorProxy] B -->|createProxy| C[Proxied External Client] C --> D[External API / SDK] D -->|Success| C C -->|Result| A D -->|Throws external error| B B -->|Normalized application error| A
Usage
tsimport { ExternalErrorProxy } from '@your-package/core/helpers/external-proxy';
const externalClient = {
async getCustomer(customerId: string) {
const response = await fetch(
`https://api.example.com/customers/${customerId}`,
);
if (!response.ok) {
throw new Error(`External API request failed: ${response.status}`);
}
return response.json();
},
};
const errorProxy = new ExternalErrorProxy();
const customerClient = errorProxy.createProxy(externalClient);
const customer = await customerClient.getCustomer('customer_123');
console.log(customer);
AI Coding Instructions
- Wrap external SDKs, HTTP clients, and integration adapters with
createProxy()instead of duplicating error-handling logic in every caller. - Preserve the original external client method signatures when adding new proxied dependencies.
- Ensure error normalization retains useful context, such as the failing operation or upstream response details, without exposing sensitive data.
- Do not bypass the proxy for production integration calls; doing so can create inconsistent error behavior across the application.
- Add tests for both successful method delegation and failures thrown by proxied external methods.
How it works
ExternalErrorProxy is a class whose createProxy method returns an asynchronous callback wrapper around a target callback. The wrapper accepts any arguments and invokes the target with those same arguments. [packages/core/helpers/external-proxy.ts:5-13]
createProxytakes a callabletargetCallback, anExternalExceptionsHandler, and an optional context-type value constrained tostring(defaulting generically toContextType). It performs no runtime validation of these inputs. [packages/core/helpers/external-proxy.ts:6-10]- On normal completion, the wrapper resolves to the value produced by
targetCallback, including when that value is a promise, because it awaits the invocation. [packages/core/helpers/external-proxy.ts:11-13] - It catches both synchronous throws and promise rejections from
targetCallback. [packages/core/helpers/external-proxy.ts:11-17] The repository test exercises both forms and expects the exception handler’snextmethod to be called. [packages/core/test/helpers/external-proxy.spec.ts:24-41] - On a caught value, it constructs an
ExecutionContextHostfrom the wrapper’s received arguments, sets its context type, and returnsexceptionsHandler.next(e, host). [packages/core/helpers/external-proxy.ts:14-18]ExecutionContextHostretains the supplied argument array; its initial type is'http', andsetTypechanges that type only when its argument is truthy. [packages/core/helpers/execution-context-host.ts:10-21,35-40] - Consequently, when
typeis omitted or otherwise falsy at runtime, the host passed to the exception handler retains the default'http'type. [packages/core/helpers/external-proxy.ts:15-17] [packages/core/helpers/execution-context-host.ts:11,19-21] ExternalExceptionsHandler.nextfirst invokes a selected custom filter when one matches; otherwise it calls the inheritedcatchmethod. [packages/core/exceptions/external-exceptions-handler.ts:11-17,26-35] The inherited path logs non-intrinsicErrorinstances and then throws the caught exception. [packages/core/exceptions/external-exception-filter.ts:6-15]ExternalContextCreatorwraps its generated external handler with this proxy only whenoptions.filtersis truthy; otherwise it returns the target handler unwrapped. [packages/core/helpers/external-context-creator.ts:164-185]
Relationships
- IMPORTS →
ContextType
Was this page helpful?