{"id":16260095,"url":"https://github.com/cludden/errlich","last_synced_at":"2025-04-08T13:50:04.181Z","repository":{"id":72389527,"uuid":"65220677","full_name":"cludden/errlich","owner":"cludden","description":"lightweight error utilities for RESTful apis","archived":false,"fork":false,"pushed_at":"2016-08-08T20:54:51.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-14T10:18:21.129Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/cludden.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"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":"2016-08-08T16:30:50.000Z","updated_at":"2016-08-08T16:31:11.000Z","dependencies_parsed_at":"2023-06-14T21:00:38.711Z","dependency_job_id":null,"html_url":"https://github.com/cludden/errlich","commit_stats":{"total_commits":5,"total_committers":1,"mean_commits":5.0,"dds":0.0,"last_synced_commit":"bd41c2e03845eb0c5b596027a48b1fc29b1b0006"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cludden%2Ferrlich","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cludden%2Ferrlich/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cludden%2Ferrlich/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cludden%2Ferrlich/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cludden","download_url":"https://codeload.github.com/cludden/errlich/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247855056,"owners_count":21007486,"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":[],"created_at":"2024-10-10T16:06:17.960Z","updated_at":"2025-04-08T13:50:04.136Z","avatar_url":"https://github.com/cludden.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# errlich\na lightweight provider of utility error handling functions for RESTful apis, using [boom](https://github.com/hapijs/boom).\n\n\n\n## Installing\ninstall\n```bash\nnpm install --save errlich\n```\n\n\n\n## Getting Started\nSee below for some common usage patterns.\n```javascript\nconst Errlich = require('errlich');\nconst errlich = new Errlich();\n\n\n// install a notifier or two\nconst logErrors = function(...args) {\n    console.error.apply(console, args);\n}\nconst bugsnag = require('bugsnag');\nbugsnag.register({ apiKey: '*******************' });\nerrlich.addNotifier(logErrors);\nerrlich.addNotifier(bugsnag.notify);\n\n\n// convert to boom error and call notifiers\nModel.find({}, function(err, results) {\n    if (err) {\n        err = errlich.initialize(err, 500);\n        errlich.notify(err);\n    }\n});\n\n\n// or better yet, we can intercept callback errors, convert them into\n// http friendly boom errors, and pass to our notifiers before passing\n// to the original callback\nModel.find({}, errlich.wrap(500, function(err, results) {\n\n}));\n\n\n// for synchronous functions that might throw, we can achieve the same\n// functionality as above using #wrapSync\nModel.findOne()\n.then(errlich.wrapSync(500, function getNestedProp(obj) {\n    return obj.nested.prop;\n}))\n.then(function(data) {\n    res.status(200).send({ data });\n})\n.catch(next);\n\n\n// you can also predefine any potential known errors and provide custom definitions\nconst errlich = new Errlich({\n    accessTokenMissing: {\n        code: 'access-token-missing',\n        status: 401,\n        title: 'Missing Access Token',\n        detail: 'Unable to locate access token'\n    },\n    cacheError: {\n        code: 'cache-error',\n        status: 500,\n        title: 'Unexpected Cache Error',\n        detail: 'There was an unexpected error while retrieving data from the cache'\n    }\n});\nerrlich.wrap('accessTokenMissing', myCallback);\n```\n\n\n\n## API\n\n### *new* Errlich(errors)\nConstructor function.\n\n###### Parameters\n| Name | Type | Description |\n| --- | --- | --- |\n| errors* | Object | error definitions |\n\n###### Example\n```javascript\nconst Errlich = require('errlich');\n\nconst errors = {\n    accessTokenMissing: {\n        status: 400,\n        title: 'Missing Access Token',\n        code: 'access-token-missing'\n    }\n};\n\nconst errlich = new Errlich(errors);\n```\n---\n\n### #addNotifier(notifier)\nInstall a notifier.\n\n###### Parameters\n| Name | Type | Description |\n| --- | --- | --- |\n| notifier* | Function | notifier function |\n\n###### Example\n```javascript\nconst bugsnag = require('bugsnag');\nbugsnag.register({apiKey: '**************'})\n\nerrlich.addNotifier(bugsnag.notify);\n```\n---\n\n### #get(name)\nreturns the error definition with the specified name or an undefined error definition.\n\n###### Parameters\n| Name | Type | Description |\n| --- | --- | --- |\n| name* | String | definition key |\n\n###### Example\n```javascript\nconsole.log(errlich.get('accessTokenMissing'));\n```\n---\n\n### #initialize(err, nameOrStatus, msg, properties)\nConvert an error into a boom error with additional definition data. See *wrap* for additional functionality.\n\n###### Parameters\n| Name | Type | Description |\n| --- | --- | --- |\n| err* | Error | the error to convert. if not an Error instance, a new error will instantiated |\n| nameOrStatus | Number,String | the name of the error definition or an http status |\n| msg | String | custom message/detail to use. if not provided, the existing error message will be used |\n| properties | Object | additional data to be included at `err.data` |\n\n###### Returns\nError - the decorated error\n\n###### Example\n```javascript\nif (err) {\n    errlich.initialize(err, 'accessTokenMissing', 'No access token was found on the request', { id: req.id });\n}\n```\n---\n\n### #notify(...args)\nPass an error to all notifiers for handling.\n\n###### Parameters\n| Name | Type | Description |\n| --- | --- | --- |\n| args* | * | all arguments will be passed to all notifiers |\n\n###### Example\n```javascript\nif (err) {\n    errlich.notify(err);\n}\n```\n---\n\n### #silentWrap(...args)\nSame as #wrap, except no notifiers will be called.\n\n\n---\n\n### #silentWrapSync(...args)\nSame as #wrapSync, except no notifiers will be called.\n\n\n---\n\n### #wrap(...args)\nWrap a callback function. If the callback is called with an error, convert it using the provided data, pass error to all notifiers, and then pass to the original callback. If no callback function is passed as an argument, a curried version of the wrapping function will be returned.\n\n###### Example\n```javascript\n// convert any error passed by #doSomethingAsync using the \"my-error\" definition\ndoSomethingAsync(errlich.wrap('my-error', function(err, data) {\n    console.log(err);\n}));\n\n// convert any error passed to a generic forbidden error.\ndoSomethingAsync(errlich.wrap(403, 'Uh oh! not allowed!', function(err, data) {\n    console.log(err);\n}));\n\nconst wrapReqError = errlich.wrap({ req: req.id });\nconst wrapMyError = wrapReqError('my-error');\ndoSomethingAsync(wrapMyError('my custom message', function(err, data) {\n    console.log(err);\n}));\n```\n---\n\n### #wrapSync(...args)\nWrap a synchronous function that could potentially throw. Convert any error thrown using the provided data, pass error to all notifiers, and then throw the converted error. If no function is passed as an argument, a curried version of the wrapping function will be returned.\n\n###### Example\n```javascript\n// convert any error passed by #doSomethingAsync using the \"my-error\" definition\nfunction findMin(values) {\n    if (!_.isArray(values) || !values.length) {\n        throw new Error('Unable to find min of non/empty array');\n    }\n    return Math.min.apply(Math, values);\n}\nTransactions.findByAccountId(id)\n.then(errlich.wrapSync(404, findMin))\n.then(function(min) {\n    res.status(200).send({ min });\n})\n.catch(next)\n```\n---\n\n\n\n## Testing\nrun tests\n```bash\nnpm test\n```\n\nrun coverage\n```bash\nnpm run coverage\n```\n\n\n\n## Contributing\n1. [Fork it](https://github.com/cludden/errlich/fork)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n\n\n\n## License\nCopyright (c) 2016 Chris Ludden. Licensed under the [MIT License](LICENSE.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcludden%2Ferrlich","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcludden%2Ferrlich","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcludden%2Ferrlich/lists"}