### Wrap Existing Functions Source: https://github.com/mcking-07/safe-wrapper/blob/main/README.md Shows how to apply the 'safe' utility to pre-existing functions, including those from third-party libraries, to add robust error handling. ```javascript import { safe } from 'safe-wrapper'; const sync = (args) => { throw new Error('sync error occurred'); } const safeSync = safe(sync); // Example usage (assuming 'args' is defined) // const [error, result] = safeSync(args); ``` -------------------------------- ### Directly Wrap Sync/Async Functions Source: https://github.com/mcking-07/safe-wrapper/blob/main/README.md Demonstrates how to use the 'safe' utility to wrap both synchronous and asynchronous functions. The wrapper returns a tuple containing an error (or null) and the result. ```javascript import { safe } from 'safe-wrapper'; const safeSync = safe((args) => { throw new Error('sync error occurred'); }); const safeAsync = safe(async (args) => { throw new Error('async error occurred'); }); // Example usage (assuming 'args' is defined) // const [error, result] = await safeAsync(args); ``` -------------------------------- ### Wrap Built-in Functions Source: https://github.com/mcking-07/safe-wrapper/blob/main/README.md Shows how to wrap standard JavaScript built-in functions like JSON.parse and Object.keys using the safe-wrapper. This allows for safer execution by catching potential errors. ```javascript import { safe } from 'safe-wrapper'; const safeJsonParse = safe(JSON.parse); const [error, result] = safeJsonParse('invalid_json'); // Example with Object.keys and error type filtering const [errorObjKeys, resultObjKeys] = safe(Object.keys, [TypeError])(null); ``` -------------------------------- ### Handle Multiple Error Types Source: https://github.com/mcking-07/safe-wrapper/blob/main/README.md Demonstrates wrapping a function with 'safe' to catch any of the specified error types. This is useful when a function might throw different kinds of errors. ```typescript import { safe } from 'safe-wrapper'; const sync = (args: boolean): string => { if (args) { throw new TypeError('sync type error occurred'); } else { throw new RangeError('sync range error occurred'); } } const safeSync = safe(sync, [TypeError, RangeError]); // Example usage (assuming 'args' is defined) // const [error, result] = safeSync(args); ``` -------------------------------- ### Handle Specific Error Types Source: https://github.com/mcking-07/safe-wrapper/blob/main/README.md Illustrates how to configure 'safe' to catch only specific error types, allowing other errors to propagate normally. This enhances control over error management. ```javascript import { safe } from 'safe-wrapper'; const safeAsync = safe(async (args) => { throw new TypeError('async type error occurred'); }, [TypeError]); // Example usage (assuming 'args' is defined) // const [error, result] = await safeAsync(args); ``` -------------------------------- ### Synchronous Custom Error Transformer Source: https://github.com/mcking-07/safe-wrapper/blob/main/README.md Shows how to provide a custom transformer function to 'safe' for synchronous operations. This allows errors to be converted into a specific format before being returned. ```typescript import { safe } from 'safe-wrapper'; type TransformedError = { code: string, message: string, timestamp: Date }; const transformer = (error: Error): TransformedError => ({ code: error.name, message: error.message, timestamp: new Date() }); const safeWithTransform = safe( () => { throw new Error('custom sync error'); }, [Error], transformer ); // Example usage: // const [error, result] = safeWithTransform(); // 'error' will be of type TransformedError: { code: 'Error', message: 'custom sync error', timestamp: Date } ``` -------------------------------- ### Async Custom Error Transformer Source: https://github.com/mcking-07/safe-wrapper/blob/main/README.md Demonstrates using an asynchronous function to transform errors caught by the safe-wrapper. The transformer receives the original error and returns a new error object with additional properties like a timestamp. ```typescript import { safe } from 'safe-wrapper'; type AsyncTransformedError = { code: string, message: string, timestamp: Date }; // Assume 'report' is an async function available in the scope // async function report(error: Error): Promise { /* ... */ } const transformer = async (error: Error): Promise => { // await report(error); return { code: error.name, message: error.message, timestamp: new Date() } } const safeWithTransform = safe( async () => { throw new Error('custom async error'); }, [Error], transformer ); const [error, result] = await safeWithTransform(); // error will be of type AsyncTransformedError as: { code: 'Error', message: 'custom async error', timestamp: Date } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.