JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 9
  • Score
    100M100P100Q57207F
  • License ISC

npm module for handling and logging errors in Node.js API

Package Exports

  • any-restapi-errors
  • any-restapi-errors/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (any-restapi-errors) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

How do you use it? Throw it in your code when you want to handle 404 or any errors (404 only for now. other errors will be covered in coming milestones).

  try {
    if (user === null) {
 throw new Api404Error(`User with id: ${req.params.id} not found.`)
}
  } catch (error) {
    // Log the error using the custom error logger
    error.errorLogger();
    res.status(error.statusCode || 500).send({
      error: {
        name: error.name,
        message: error.message,
        statusCode: error.statusCode,
        isOperational: error.isOperational
      }
    });
  }

Error Logger

error logger

  • when you use error.errorLogger(); method it will create error.log file and logs details like isOperational, level, message, service,stack.
  • Following Json object example will be created in error.log file when error will occure.

{"isOperational":true,"level":"error","message":"Not found.","service":"user-service","stack":"Error: Not found.\n at new BaseError (file:///D:/npm-error/baseError.js:11:8)\n ,"statusCode":404}