Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 41 lines 1.4 KB
Raw
1 import logger from './logger';
2
3 /**
4 * Describes a thrown or rejected value for a log line without ever throwing.
5 *
6 * The logger serializes object arguments with `JSON.stringify`, which turns an `Error` into `{}`
7 * and throws on a circular value, so callers log this string instead of the raw value.
8 * `JSON.stringify` and `String()` can both throw (a circular value, an object without a
9 * prototype), so each is tried in turn.
10 */
11 export function describeThrown(value: unknown): string {
12 try {
13 if (value instanceof Error) return value.message;
14 if (typeof value === 'object' && value !== null) return JSON.stringify(value) ?? String(value);
15 return String(value);
16 } catch {
17 try {
18 return String(value);
19 } catch {
20 return '[unprintable value]';
21 }
22 }
23 }
24
25 /**
26 * Calls a user-supplied event callback so that neither a synchronous throw nor a rejected promise
27 * escapes into the event emitter that invoked it. Failures are logged after `label`.
28 *
29 * pusher-js does not catch listener exceptions, and nothing awaits the promise an `async`
30 * listener returns, so an escaped failure would end a Node process.
31 */
32 export function invokeUserCallback(label: string, callback: () => unknown): void {
33 const logFailure = (error: unknown) => {
34 logger.error(label, describeThrown(error));
35 };
36 try {
37 Promise.resolve(callback()).catch(logFailure);
38 } catch (error) {
39 logFailure(error);
40 }
41 }