
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
safe-catch
Advanced tools
`SafeCatch` is an NPM library written in TypeScript that provides two utility functions, `tryCatchPromise` and `tryCatchFunction`, to simplify error handling in asynchronous and synchronous code. This library aims to make error handling more concise and r
SafeCatch is an NPM library written in TypeScript that provides two utility functions, tryCatchPromise and tryCatchFunction, to simplify error handling in asynchronous and synchronous code. This library aims to make error handling more concise and readable by encapsulating the try-catch logic within these functions.
You can install SafeCatch using npm:
npm install safe-catch
tryCatchPromiseThe tryCatchPromise function is designed for handling errors in asynchronous operations wrapped in Promises.
/* imports... */
import { tryCatchPromise, hasError } from "safe-catch";
function fetchData() {
return axios.get('https://jsonplaceholder.typicode.com/todos/1')
}
async function example() {
const [error, result] = await tryCatchPromise(fetchData());
if (hasError(error, result)) {
return console.error("An error occurred:", error);
}
return console.log("Data fetched:", result.data);
}
example();
In the example above, tryCatchPromise is used to handle the asynchronous operation fetchData(). It returns a tuple [result, error], where result contains the result of the successful operation or null if an error occurred, and error contains the error object or null if the operation was successful.
tryCatchFunctionThe tryCatchFunction is designed for handling errors in synchronous functions.
import { tryCatchFunction, hasError } from "safe-catch";
function divide(a: number, b: number) {
if (b === 0) {
throw new Error("Division by zero");
}
return a / b;
}
function example() {
const [error, result] = tryCatchFunction(() => divide(10, 2));
if (hasError(error, result)) {
return console.log('Something went wrong :(', error)
}
return console.log('Result', result)
}
example();
In the above example, tryCatchFunction wraps the synchronous function divide. It also returns a tuple [result, error], where result contains the return value of the function if it executed successfully, or null if an error occurred. The error variable holds the error object or null if the function was executed without any errors.
SafeCatch eliminates the need for writing repetitive and verbose try-catch blocks. This leads to cleaner and more readable code.SafeCatch promotes a consistent approach to error handling across asynchronous and synchronous code. It provides a uniform way to handle errors, making the codebase easier to understand and maintain.SafeCatch is a TypeScript library that simplifies error handling in asynchronous and synchronous code by encapsulating the try-catch logic within utility functions. It helps improve code readability, reduces boilerplate, and promotes consistent error handling practices. By using SafeCatch, developers can write cleaner and more maintainable code while effectively handling errors.
FAQs
`SafeCatch` is an NPM library written in TypeScript that provides two utility functions, `tryCatchPromise` and `tryCatchFunction`, to simplify error handling in asynchronous and synchronous code. This library aims to make error handling more concise and r
We found that safe-catch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.