tryon
v1.0.1
Published
Simplifies error control in your JavaScript/TypeScript
Downloads
7
Readme
Overview
Currently, there are basically two options to catch errors in your JavaScript code:
Option 1: try/catch for a) synchronous code or b) code using the async/await keywords:
try {
// code logic here
} catch (error) {
// error handling here
}
Option 2: catch handler when using a Promise:
promise
.then((result) => {
// code logic here
})
.catch((error) => {
// error handling here
});
However, these default approaches have some issues:
- verbosity
- different options to treat errors (sync x promise)
- no default error handling
What if we could simplify the error control, with a cleaner usage, and also have a default error handling? This is the goal of the tryon package and here it is how you can use it:
tryon(() => {
// code logic here
});
The simple example below demonstrates it with a real code. See below for additional examples.
// You just need to wrap your code within a function
tryon(() => {
// Your code starts here
function throwAnError() {
throw new Error("This error should be fired");
}
throwAnError();
console.log("This code should never run");
});
The above code will gracefully run with the error being printed to the console:
This error should be fired
How to install:
npm install tryon
Examples
import tryon, { changeErrorFn } from "tryon";
const newErrorFn = (error) => {
console.log("It works!!! The error is:", error.message);
};
changeErrorFn(newErrorFn);
tryon(() => {
function throwAnError() {
throw new Error("This error should be fired");
}
throwAnError();
console.log("This code should never run");
});
import tryon, { changeErrorFn } from "tryon";
const newErrorFn = (error) => {
console.log("It works!!! The error is:", error.message);
};
changeErrorFn(newErrorFn);
await tryon(async () => {
function throwAnError() {
throw new Error("This error should be fired");
}
const p = new Promise((resolve, reject) => {
throwAnError();
console.log("This code should never run");
resolve(true);
});
const value = await p; // Promise throws an error
console.log("This code should never run");
});
import tryon from "tryon";
await tryon(() => {
function throwAnError() {
throw new Error("This error should be fired");
}
const p = new Promise((resolve, reject) => {
throwAnError();
console.log("This code should never run");
resolve(true);
});
// It is necessary to return the promise, to sync with the external await,
// otherwise the promise will be unsynced and can not be catched.
return p;
});
import tryon from "tryon";
await tryon(async () => {
const p = new Promise((resolve, reject) => {
reject(false);
});
await p; // Promise will reject
console.log("This code should never run");
});
import tryon from "tryon";
tryon(
() => {
function throwAnError() {
throw new Error("This error should be fired");
}
throwAnError();
console.log("This code should never run");
},
(error) => {
console.log("It works!!! The error is:", error.message);
}
);
🛠 Technologies
The following libraries/frameworks were used on the project:
Author
Done with ❤️ by Marcos Pereira 👋🏽 Contact me!