{"id":20348480,"url":"https://github.com/onebeyond/error-handler-module","last_synced_at":"2025-04-12T01:15:01.553Z","repository":{"id":35497409,"uuid":"218024042","full_name":"onebeyond/error-handler-module","owner":"onebeyond","description":"Error handler methods for Node.js app and express middlewares.","archived":false,"fork":false,"pushed_at":"2023-07-19T10:45:49.000Z","size":1022,"stargazers_count":3,"open_issues_count":5,"forks_count":3,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-02T13:17:41.642Z","etag":null,"topics":["error-handler","express","middleware-handlers"],"latest_commit_sha":null,"homepage":"https://onebeyond.github.io/error-handler-module/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/onebeyond.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-28T10:43:41.000Z","updated_at":"2023-01-27T08:13:36.000Z","dependencies_parsed_at":"2024-06-20T23:36:11.648Z","dependency_job_id":null,"html_url":"https://github.com/onebeyond/error-handler-module","commit_stats":null,"previous_names":["guidesmiths/error-handler-module"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onebeyond%2Ferror-handler-module","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onebeyond%2Ferror-handler-module/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onebeyond%2Ferror-handler-module/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onebeyond%2Ferror-handler-module/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/onebeyond","download_url":"https://codeload.github.com/onebeyond/error-handler-module/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248262407,"owners_count":21074303,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["error-handler","express","middleware-handlers"],"created_at":"2024-11-14T22:20:39.872Z","updated_at":"2025-04-12T01:15:01.534Z","avatar_url":"https://github.com/onebeyond.png","language":"JavaScript","readme":"\n# error-handler-module\n\n![npm](https://img.shields.io/npm/v/error-handler-module)\n[![CircleCI](https://circleci.com/gh/guidesmiths/error-handler-module.svg?style=svg)](https://circleci.com/gh/guidesmiths/error-handler-module)\n[![Maintainability](https://api.codeclimate.com/v1/badges/ae7c4688ac9ca251b5d4/maintainability)](https://codeclimate.com/github/onebeyond/error-handler-module/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/ae7c4688ac9ca251b5d4/test_coverage)](https://codeclimate.com/github/onebeyond/error-handler-module/test_coverage)\n\nThis module provides a way to handle error in an express app with a few methods that creates error and an express error middleware `handleHttpError`.\n\n1. [Installation](#Installation)\n2. [Basic Error types](#Basic-Error-types)\n3. [Methods](#methods)\n  - [errorFactory](#errorFactory)\n  - [handleHttpError](#handleHttpError)\n  - [tagError](#tagError)\n4. [Implementation example](#Implementation-example)\n5. [How to debug the errors](#Debug)\n\n## Installation\n\n```\nnpm install --save error-handler-module\n```\n\n## Basic Error types\n\nWe have setup some errors that we use often use in our projects and we store them in an object which is this:\n\n```js\nconst CustomErrorTypes = {\n  BAD_REQUEST: 'bad_request',\n  FORBIDDEN: 'forbidden',\n  NOT_FOUND: 'not_found',\n  OAS_VALIDATOR: 'OpenAPIUtilsError:response',\n  SWAGGER_VALIDATOR: 'swagger_validator', // Deprecated\n  UNAUTHORIZED: 'unauthorized',\n  WRONG_INPUT: 'wrong_input',\n};\n```\n\n*NOTE* If you need another kind of error keep reading on [tagError method](#tagError).\n\n## Methods\n\n### errorFactory(type: String) =\u003e (message: String)\n\nThis function creates a custom Error with a type and a message you decide.\n\n**Example**\n\n```js\nconst { errorFactory, CustomErrorTypes } = require('error-handler-module');\n\n// With Basic Errors\nconst wrongInputError = errorFactory(CustomErrorTypes.WRONG_INPUT);\nwrongInputError('Error message');\n\n/* returns\n  CustomError {\n    name: 'CustomError',\n    type: 'wrong_input',\n    message: 'Error message'\n  }\n*/\n\n// with your custom types\n\n// With Basic Errors\nconst customTypeError = errorFactory('custom-type');\ncustomTypeError('Error message');\n\n/* returns\n  CustomError {\n    name: 'CustomError',\n    type: 'custom-type',\n    message: 'Error message'\n  }\n*/\n```\n\n### handleHttpError(logger: Object, metrics: Object) =\u003e (err, req, res, next)\n\nThis is a function which will work as a middleware for your express app so that your errors will response with an HTTP response.\n\nYou have to pass logger and metrics objects as parameters (*Metrics is **not required***).\n\n**Example**\n\n```js\nconst express = require('express');\nconst { handleHttpError } = require('error-handler-module');\n\nconst app = express();\n\napp.use(handleHttpError(logger, metrics));\n```\n\n### tagError(error: Error, newTypes: Object)\n\nThis function is going to tag your errors from `CustomError` to `HTTPErrors`so that handleHttpError middleware will understand them.\n\nIt receives error and newTypes (*not required*)\n\n*NewTypes object*\n\nIn order to add new error types, this object must match this structure:\n\n```js\nconst newTypes = {\n  \u003cError_type_string\u003e: \u003cstatus_code_number\u003e,\n  \u003cError_type_string\u003e: \u003cstatus_code_number\u003e,\n  ...\n};\n````\n\n**Example**\n\n```js\nconst { errorFactory } = require('error-handler-module');\n\n// With Custom Errors\nconst customTypeError = errorFactory('tea-pot-error');\nconst error = customTypeError('Error message');\n\n// const new Type\nconst newTypes = {\n  'tea-pot-error': 418,\n};\n\n\ntagError(error, newTypes);\n/* This returns\n  CustomHTTPError {\n    name: 'CustomHTTPError',\n    statusCode: 418,\n    message: 'Error message',\n    extra: undefined\n  }\n*/\n// This tagError will be added to the catch of the enpoints like this\nreturn next(tagError(error, newTypes))\n\n// With Basic Errors\n\n// With Custom Errors\nconst customTypeError = errorFactory(CustomErrorTypes.WRONG_INPUT);\nconst error = customTypeError('Error message');\n\ntagError(error);\n/* This returns\n  CustomHTTPError {\n    name: 'CustomHTTPError',\n    statusCode: 400,\n    message: 'Error message',\n    extra: undefined\n  }\n*/\n```\n\n### Implementation example\n\n```js\nconst express = require('express');\nconst {\n  CustomErrorTypes,\n  errorFactory,\n  handleHttpError,\n  tagError,\n} = require('error-handler-module');\n\nconst app = express();\n\nconst loggerMock = {\n  error: () =\u003e '',\n};\n\napp.get('/test-error-basic', (req, res, next) =\u003e {\n  // creating tagged error\n  const wrongInputError = errorFactory(CustomErrorTypes.NOT_FOUND);\n  try {\n    throw wrongInputError('Wrong Input message');\n  } catch (error) {\n    return next(tagError(error));\n  }\n});\n\napp.get('/test-error-extended', (req, res, next) =\u003e {\n  // creating a custom tag error\n  // for example custom db-error\n  const dbError = errorFactory('db-access-not-allowed');\n  /*\n   * New types must be objects with error and a valid status code\n   */\n  const newErrors = {\n    'db-access-not-allowed': 401,\n  };\n  try {\n    throw dbError('db Error');\n  } catch (error) {\n    return next(tagError(error, newErrors));\n  }\n});\n\napp.use(handleHttpError(loggerMock));\n\nmodule.exports = app;\n```\n\n### Debug\n\nThis project uses Debug if you want more info of your error please run `DEBUG=error-handler-module \u003cYour_npm_script\u003e`.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonebeyond%2Ferror-handler-module","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fonebeyond%2Ferror-handler-module","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonebeyond%2Ferror-handler-module/lists"}