{"id":31646088,"url":"https://github.com/codefresh-io/cf-errors","last_synced_at":"2025-10-07T05:19:57.692Z","repository":{"id":31220192,"uuid":"34781291","full_name":"codefresh-io/cf-errors","owner":"codefresh-io","description":"Extensible error library","archived":false,"fork":false,"pushed_at":"2023-07-19T01:02:47.000Z","size":402,"stargazers_count":15,"open_issues_count":9,"forks_count":6,"subscribers_count":35,"default_branch":"develop","last_synced_at":"2025-08-17T23:50:01.900Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://codefresh-io.github.io/cf-errors/","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/codefresh-io.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-04-29T08:17:28.000Z","updated_at":"2023-02-27T09:18:11.000Z","dependencies_parsed_at":"2024-06-18T16:57:53.273Z","dependency_job_id":null,"html_url":"https://github.com/codefresh-io/cf-errors","commit_stats":null,"previous_names":["codefresh/cf-errors"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/codefresh-io/cf-errors","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codefresh-io%2Fcf-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codefresh-io%2Fcf-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codefresh-io%2Fcf-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codefresh-io%2Fcf-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codefresh-io","download_url":"https://codeload.github.com/codefresh-io/cf-errors/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codefresh-io%2Fcf-errors/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278722757,"owners_count":26034463,"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","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2025-10-07T05:19:55.945Z","updated_at":"2025-10-07T05:19:57.681Z","avatar_url":"https://github.com/codefresh-io.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"cf-errors\r\n===========\r\n[![Coverage Status](https://coveralls.io/repos/github/codefresh-io/cf-errors/badge.svg?branch=develop)](https://coveralls.io/github/codefresh-io/cf-errors?branch=develop)\r\n\r\nExtensible error library.\r\n\r\n##Installation\r\n```javascript\r\n$ npm install cf-errors\r\n```\r\n\r\n* [Creating an error](#constructor)\r\n* [Extending with a previous error](#cause)\r\n* [Printing the stack](#stack)\r\n* [toString](#toString)\r\n* [Predefined errors](#predefined)\r\n* [Inheriting the previous error type](#inherit)\r\n* [Getting the value of the first occurrence of a field in the chain](#getfirstvalue)\r\n* [Running the tests](#tests)\r\n\r\n\r\n\u003ca name=\"constructor\" /\u003e\r\n\r\n##Creating an error\r\n```javascript\r\nvar CFError = require('cf-errors');\r\nvar error   = new CFError(\"error message\");\r\n```\r\n\r\n###Extending the error\r\n```javascript\r\nvar error = new CFError({field: \"value\", message: `error message`});\r\n```\r\n\r\n###Setting the error name\r\n```javascript\r\nvar error = new CFError({name: \"ErrorType\", message: \"my error name\"});\r\n```\r\n\r\n###Passing multiple objects will extend the previous objects\r\n```javascript\r\nvar error = new CFError({field: \"value\", message: `error message`}, {field2: \"value\"}, {field: \"override first value\"});\r\n```\r\n\r\n###Last argument passed to the constructor can be a string, which will populate the message field automatically\r\n```javascript\r\nvar error = new CFError({field: \"value\", message: `error message`}, {field2: \"value\"}, \"my error message\");\r\n```\r\n\r\n\u003ca name=\"cause\" /\u003e\r\n\r\n##Extending with a previous error\r\n```javascript\r\nvar extendedError = new CFError({\r\n    message: `extended error message`,\r\n    cause: error\r\n});\r\n```\r\n\r\n\u003ca name=\"stack\" /\u003e\r\n\r\n##Printing the stack\r\nwill print the stack of all previous errors too\r\n```javascript\r\nconsole.log(extendedError.stack);\r\n```\r\n\r\n\u003ca name=\"toString\" /\u003e\r\n\r\n##toString()\r\nWill print the whole chain of errors in a nice way. \u003c/br\u003e\r\nYou can always override it if you want.\r\n```javascript\r\nCFError.prototype.toString = function(){\r\n    //your implementation\r\n}\r\n```\r\n\r\n\u003ca name=\"predefined\" /\u003e\r\n\r\n##Predefined Error Types\r\n```javascript\r\nvar CFError    = require('cf-errors');\r\nvar Errors     = CFError.Errors;\r\n```\r\nAll predefined errors are exposed on 'CFError.Errors' object. \u003c/br\u003e\r\nThey are actually just simple objects so using the extension capability allows us to use them easily and extend them when needed.\r\n####Http Errors\r\nAll http errors are available.\r\nThey will contain a field name 'statusCode' for your use.\r\n```javascript\r\nvar error = new CFError(Errors.Http.BadRequest, {\r\n    message: `failed to validate your request`\r\n});\r\n```\r\nIf you are using express.js then your error middleware can look something like this:\r\n```javascript\r\napp.use(function(err, request, response, next){\r\n    console.error(err.stack);\r\n    var statusCode = 400;\r\n    if (err.constructor \u0026\u0026 err.constructor.name === \"CFError\") {\r\n        statusCode = err.statusCode || statusCode;\r\n    }\r\n    return response.status(statusCode).send(err.message);\r\n});\r\n```\r\n####Node Errors\r\nAll node.js core errors are also available using the Errors.Node object.\r\n\r\n\u003ca name=\"inherit\" /\u003e\r\n\r\n##Inheriting the previous error type\r\nCreating an error with the same name as its cause can be achieved using 'Inherit' as the error name.\r\n```javascript\r\nvar extendedError = new CFError(Errors.Inherit, {\r\n    message: `extended error message`,\r\n    cause: error\r\n});\r\n```\r\nThis will also work\r\n```javascript\r\nvar extendedError = new CFError({\r\n    name: \"Inherit\",\r\n    message: `extended error message`,\r\n    cause: error\r\n});\r\n```\r\n\r\n\u003ca name=\"getfirstvalue\" /\u003e\r\n\r\n##Getting the value of the first occurrence of a field in the chain\r\nSometimes you will populate an error with a field and wrap it with an additional error. In order to get the value of the field you will need to recursively go over the whole chain. \u003c/br\u003e\r\nIn order to get the first value of a field in the chain use 'getFirstValue' function.\r\n```javascript\r\nvar error = new CFError({field: \"value\", field1: \"firstValue\"});\r\nvar extendedError = new CFError({cause: error, field1: \"newValue\"});\r\nextendedError.getFirstValue('field') // \"value\"\r\nextendedError.getFirstValue('field1') // \"newValue\"\r\nextendedError.getFirstValue('field2') // undefined\r\n```\r\n\r\n\u003ca name=\"tests\" /\u003e\r\n\r\n##Running the tests\r\n'npm test' or 'gulp unit_test'\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodefresh-io%2Fcf-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodefresh-io%2Fcf-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodefresh-io%2Fcf-errors/lists"}