{"id":13716454,"url":"https://github.com/onbjerg/micro-boom","last_synced_at":"2025-05-07T05:33:08.925Z","repository":{"id":57296381,"uuid":"79716221","full_name":"onbjerg/micro-boom","owner":"onbjerg","description":"Wraps errors in micro with Boom","archived":true,"fork":false,"pushed_at":"2020-09-04T03:20:37.000Z","size":391,"stargazers_count":35,"open_issues_count":4,"forks_count":12,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-04T03:57:49.400Z","etag":null,"topics":["boom","json-api","micro","standards","zeit"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/onbjerg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-22T12:24:07.000Z","updated_at":"2023-08-15T16:34:14.000Z","dependencies_parsed_at":"2022-09-07T03:20:50.737Z","dependency_job_id":null,"html_url":"https://github.com/onbjerg/micro-boom","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onbjerg%2Fmicro-boom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onbjerg%2Fmicro-boom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onbjerg%2Fmicro-boom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onbjerg%2Fmicro-boom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/onbjerg","download_url":"https://codeload.github.com/onbjerg/micro-boom/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252823458,"owners_count":21809704,"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":["boom","json-api","micro","standards","zeit"],"created_at":"2024-08-03T00:01:10.650Z","updated_at":"2025-05-07T05:33:08.640Z","avatar_url":"https://github.com/onbjerg.png","language":"JavaScript","funding_links":[],"categories":["Modules"],"sub_categories":["Wrappers"],"readme":"# `micro-boom` [![NPM](https://img.shields.io/npm/v/micro-boom.svg?style=flat)](https://www.npmjs.org/package/micro-boom) [![travis-ci](https://travis-ci.org/onbjerg/micro-boom.svg?branch=master)](https://travis-ci.org/onbjerg/micro-boom) [![Greenkeeper](https://badges.greenkeeper.io/onbjerg/micro-boom.svg)](https://greenkeeper.io/)\n\nWraps errors in [`micro`](https://github.com/zeit/micro) services [`Boom`](https://github.com/hapijs/boom) errors.\n\n**Example Responses**\n\n```json\n{\n  \"error\": \"Unauthorized\", \n  \"message\": \"Not authenticated\", \n  \"statusCode\": 401\n}\n```\n\n```json\n{\n  \"data\": {\n    \"reason\": \"Username is wrong\"\n  }, \n  \"error\": \"Unauthorized\", \n  \"message\": \"Not authenticated\", \n  \"statusCode\": 401\n}\n```\n\n---\n\n## Installation\n\n```sh\nnpm install --save micro-boom\n```\n\nOr even better\n\n```sh\nyarn add micro-boom\n```\n\n## Import and Usage Example\n\n```js\nconst { handleErrors, createError } = require('micro-boom')\n\nmodule.exports = handleErrors(async function (req, res) {\n  throw createError(401, 'Not authenticated', {\n    reason: 'Bad password'\n  })\n})\n```\n\n## API\n\n#### handleErrors\n\nCatches error from an async function, wraps them in a Boom error object and generates a JSON response.\n\nThe status code of an error is determined by three factors, in order:\n\n- Status code is set to `err.output.statusCode`\n- If not set, error is inferred from `res.statusCode`\n- Default to HTTP 500 (also defaults to HTTP 500 if status is \u003c 400)\n\n\u003e :rotating_light: **TAKE NOTE** :rotating_light:  \n\u003e All HTTP 500 errors have their user provided message removed for security reasons.\n\n**Parameters**\n\n-   `fn` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Async function, your normal `micro` logic.\n-   `dump` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Optional. Dumps `err.stack` to `stderr` if true\n\n**Examples**\n\n```js\nconst { handleErrors } = require('micro-boom')\n\n// Returns HTTP 500\nmodule.exports = handleErrors(async function (req, res) {\n  throw Error('Uh-oh, something bad happened.')\n})\n```\n\n```js\nconst { handleErrors } = require('micro-boom')\n\n// Returns HTTP 401\nmodule.exports = handleErrors(async function (req, res) {\n  res.statusCode = 401\n  throw Error('Unauthorized')\n})\n```\n\nReturns an async **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)**.\n\n#### createError\n\nAlias for [`Boom#create(statusCode, [message], [data])`](https://github.com/hapijs/boom#createstatuscode-message-data).\n\n**Parameters**\n\n-   `statusCode` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** HTTP status code, must be \u003e= 400\n-   `message` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** An optional error message.\n-   `data` **[Any]** Some optional error metadata, serialized with [``JSON.stringify``](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).\n\n**Examples**\n\n```js\nconst { handleErrors, createError } = require('micro-boom')\n\n// Message defaults to what corresponds to the HTTP error code\nmodule.exports = handleErrors(async function (req, res) {\n  throw createError(500)\n})\n```\n\n```js\nconst { handleErrors, createError } = require('micro-boom')\n\n// HTTP 401: Unauthorized\nmodule.exports = handleErrors(async function (req, res) {\n  throw createError(401, 'Unauthorized')\n})\n```\n\n```js\nconst { handleErrors, createError } = require('micro-boom')\n\n// HTTP 401: Unauthorized with metadata,\n// set in `.data` of the response.\nmodule.exports = handleErrors(async function (req, res) {\n  throw createError(401, 'Unauthorized', {\n    reason: 'Bad password',\n    foo: 'bar'\n  })\n})\n```\n\nReturns an async **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)**.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonbjerg%2Fmicro-boom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fonbjerg%2Fmicro-boom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonbjerg%2Fmicro-boom/lists"}