Skip to content

marcosluizfp/tryon

Repository files navigation

🚀 Simplifies error control in your JavaScript/TypeScript

MIT License Badge


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


1. Normal code, plus error handling customization:
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");
});

2. Async code with await, plus error handling customization:
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");
});

3. Async code with a non awaited Promise, plus error handling customization:
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;
});

4. Async code with await on a Promise which rejects (instead of throwing an error):
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");
});



5. Error handling as a second function:
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


Marcos Pereira
🚀

Done with ❤️ by Marcos Pereira 👋🏽 Contact me!

Linkedin Badge Gmail Badge

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published