webux-errorhandler

module to manage the errors


License
LPL-1.02
Install
npm install webux-errorhandler@1.4.0

Documentation

Webux error handler

This module allows to manage the global error and to throw custom error format.

Installation

npm i --save webux-errorhandler

Usage

What is the Global Error Handler goal ?

This function handle all the errors that are threw in a router, meaning,

Example with then/catch :

const app = express();
const { globalErrorHandler, errorHandler } = require("webux-errorhandler");

app.use("/error", (req, res, next) => {
  setImmediate(() => {
    next(
      errorHandler(
        508,
        "test is an error !",
        { why: "because it's a test" },
        "dev message"
      )
    );
  });
});

globalErrorHandler(app)
  .then(loaded => {
    console.log(loaded);

    app.listen(1337, () => {
      console.log("Server is listening on port 1337");
    });
  })
  .catch(e => {
    console.error("Webux-errorhandler - " + e.message);
  });

NOTE This example is available in the example 4/index.js file

The errorHandler() Function will create a new custom error to get a standardized way to throw errors,
By accessing the /error resource, you will get an error that will be handle by the globalErrorHandler() Function

Example with async/wait

async function loadApp() {
  try {
    app.get("/test", (req, res, next) => {
      setImmediate(() => {
        next(
          errorHandler(
            508,
            "test is an error !",
            { why: "because it's a test" },
            "dev message"
          )
        );
      });
    });

    const loaded = await globalErrorHandler(app, log);

    if (!loaded) {
      throw new Error("Global Error Handler Not loaded.");
    }

    log.debug(loaded);

    app.listen(1337, () => {
      log.info("Server is listening on port 1337");
    });
  } catch (e) {
    console.error(e);
    throw e;
  }
}

try {
  loadApp();
} catch (e) {
  process.exit(1);
}

NOTE This example is available in the example 3/index.js file

What is the error handler goal ?

This function only format the error to get a normalized error,
to be able to use the error return and log it in a remote logging server.
This is recommended to use it with the globalErrorHandler() function.

Usages

globalErrorHandler(app, log=console)

app - An express application
log - A custom logger function

errorHandler(code, msg, extra, devMsg)

code - HTTP Code
msg - Humain Readable Error Message
extra - An optional field to put custom information in JSON format
devMsg - Message for debugging

Examples

Please check the 4 examples provided in the examples/ folder. The express Module is used to create the API Server in all cases.

  • Example 1
    • How to use Webux-errorhandler with async/await and express
  • Example 2
    • How to use Webux-errorhandler with async/await and express, but this one will fail at initialization.
    • The custom logger is undefined
  • Example 3
    • How to use Webux-errorhandler with async/await and express,
    • Along with a custom express response (res.custom())
    • And a custom logger (log.info(), log.error(), log.debug())
  • Example 4
    • How to use Webux-errorhandler with then/catch and express,

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

SEE LICENSE IN license.txt