{"id":23274154,"url":"https://github.com/buzut/bad-request-error","last_synced_at":"2026-04-27T22:31:25.332Z","repository":{"id":34923880,"uuid":"191130539","full_name":"Buzut/bad-request-error","owner":"Buzut","description":"Custom error constructor for HTTP errors","archived":false,"fork":false,"pushed_at":"2022-02-12T01:47:08.000Z","size":106,"stargazers_count":0,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-12T16:27:25.591Z","etag":null,"topics":["http-errors","http-server","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Buzut.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":"2019-06-10T08:45:41.000Z","updated_at":"2020-10-26T15:14:19.000Z","dependencies_parsed_at":"2022-08-08T03:00:25.500Z","dependency_job_id":null,"html_url":"https://github.com/Buzut/bad-request-error","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Buzut%2Fbad-request-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Buzut%2Fbad-request-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Buzut%2Fbad-request-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Buzut%2Fbad-request-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Buzut","download_url":"https://codeload.github.com/Buzut/bad-request-error/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247471390,"owners_count":20944154,"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":["http-errors","http-server","nodejs"],"created_at":"2024-12-19T20:12:22.070Z","updated_at":"2026-04-27T22:31:25.291Z","avatar_url":"https://github.com/Buzut.png","language":"JavaScript","readme":"# bad-request-error\nBadRequestError is a custom error constructor for HTTP errors (bad parameters, forbidden, unauthorized…).\nIt takes an error message as first parameter and optionally a [HTTP error code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_errors) as second parameter (defaults to `400`).\n\n## Installation\nIt requires at least Node V6 as it uses JavaScript classes.\n\n```\nnpm install --save bad-request-error\n```\n\n## Usage\nIn addition to the [native `Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) constructor properties, `BadRequestError` adds `httpStatus` and has its `name` property set to `BadRequestError`.\n\n```js\nconst BadRequestError = require('bad-request-error');\n\nasync function addComment(req, res) {\n    await const isCorrectPasswd = checkPasswd(req);\n    if (!isCorrectPasswd) return throw new BadRequestError('Your password looks wrong', 401);\n\n    // whatever you have to do if password is correct\n}\n```\n\nIt's very convenient to use it with an error handler function like the following:\n\n```js\n// /lib/handleError.js\n\n/**\n * Send client err if it's validation error or log if it's a native or low level error\n * @param { Object } err error object\n * @param { Object } res\n */\nfunction handleError(err, res) {\n    // send errmsg to user if it's a BadRequestError\n    if (res \u0026\u0026 err.name \u0026\u0026 err.name === 'BadRequestError') {\n        res.status(err.httpStatus).json({ error: err.message });\n        return;\n    }\n\n    // send http err if res object is provided\n    if (res) res.status(500).send('Server Error');\n\n    // if it's more low level, or if errorField isn't an error's propt\n    console.error(err); // or custom logger like winston\n}\n\nmodule.exports = handleError;\n```\n\nIf using promises, the error is easily handled in a catch block.\n\n```js\nconst BadRequestError = require('bad-request-error');\nconst handleError = require('/lib/handleError');\n\n// this function return a promise with the same BadRequestError as above if password doesn't match\n// BadRequestError('Your password looks wrong', 401);\nfunction checkPasswd(passwd) {}\n\nfunction addComment(req, res) {\n    checkPasswd(req.body.passwd)\n    .then(() =\u003e {\n        // whatever you have to do if password is correct\n    })\n    .catch(err =\u003e handleError(err, res));\n}\n```\n\n## Contributing\nThere's sure room for improvement, so feel free to hack around and submit PRs!\nPlease just follow the style of the existing code, which is [Airbnb's style](http://airbnb.io/javascript/) with [minor modifications](.eslintrc).\n\nTo maintain things clear and visual, please follow the [git commit template](https://github.com/Buzut/git-emojis-hook).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuzut%2Fbad-request-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbuzut%2Fbad-request-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuzut%2Fbad-request-error/lists"}