Iglo

Iglo quick error handling and catching

Instalation

npm install @mvdlei/iglo

Usage

Use it in any environment to catch errors and handle unexpected exceptions.

import { Iglo } from "@mvdlei/iglo";
 
const iglo = Iglo.init({
  // default options:
  runtime: "node", // set to browser when not using next server or node
  catchGlobal: true,
  log: logger.error, // uses @mvldei/log, a pretty wrapper on the default console
});
 
// fish and then catch errors thrown in .shell with the .fish function.
iglo.fish(Error, (e: Error) => {
  // .. handle the error
});
 
// you can also use a custom error class, just make sure it has a public 'name' property
iglo.fish(MyErrorClass, (e: MyErrorClass) => {
  // handle your custom error thrown in a .shell function
});
 
// start a shell to start catching errors
const res: ReturnType<typeof myProgramFunction> = await iglo.shell(myProgramFunction);
 
// res will be undefined when an error is thrown. for better dev experience the returned type does not include undefined, you can add it yourself for better typesafety.
console.log(res);
 
// when you are done catching errors, you can melt the iglo with .melt
iglo.melt(
  0 /** or 1 */,
  true /** to stop the execution, only works on node, default is true so set to false for usage in next and express etc*/,
);