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.

import {api404Error} from './index.js'; 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 } }); } ...

  • when you use error.errorLogger(); method it will create error.log file and logs details like isOperational, level, message, service,stack. {"isOperational":true,"level":"error","message":"Not found.","service":"user-service","stack":"Error: Not found.\n at new BaseError (file:///D:/projects/chitkara/npm-error/baseError.js:11:8)\n at new Api404Error (file:///D:/projects/chitkara/npm-error/api404Error.js:26:2)\n at file:///D:/projects/chitkara/npm-error/test-server.js:13:11\n at Layer.handle [as handle_request] (D:\\projects\\chitkara\\node_modules\\express\\lib\\router\\layer.js:95:5)\n at next (D:\\projects\\chitkara\\node_modules\\express\\lib\\router\\route.js:149:13)\n at Route.dispatch (D:\\projects\\chitkara\\node_modules\\express\\lib\\router\\route.js:119:3)\n at Layer.handle [as handle_request] (D:\\projects\\chitkara\\node_modules\\express\\lib\\router\\layer.js:95:5)\n at D:\\projects\\chitkara\\node_modules\\express\\lib\\router\\index.js:284:15\n at Function.process_params (D:\\projects\\chitkara\\node_modules\\express\\lib\\router\\index.js:346:12)\n at next (D:\\projects\\chitkara\\node_modules\\express\\lib\\router\\index.js:280:10)","statusCode":404}