{"id":26716618,"url":"https://github.com/javascript-studio/fail","last_synced_at":"2025-07-31T08:09:39.367Z","repository":{"id":57679337,"uuid":"90781910","full_name":"javascript-studio/fail","owner":"javascript-studio","description":"🚨 Fail with an Error object and a conventional code property","archived":false,"fork":false,"pushed_at":"2024-07-04T11:26:56.000Z","size":688,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-10T23:08:23.653Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/javascript-studio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-09T19:01:20.000Z","updated_at":"2024-07-04T11:26:59.000Z","dependencies_parsed_at":"2022-08-24T19:40:17.539Z","dependency_job_id":null,"html_url":"https://github.com/javascript-studio/fail","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/javascript-studio%2Ffail","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javascript-studio%2Ffail/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javascript-studio%2Ffail/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javascript-studio%2Ffail/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/javascript-studio","download_url":"https://codeload.github.com/javascript-studio/fail/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245873105,"owners_count":20686531,"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":"2025-03-27T15:38:05.737Z","updated_at":"2025-03-27T15:38:06.358Z","avatar_url":"https://github.com/javascript-studio.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  Studio Fail\n\u003c/h1\u003e\n\u003cp align=\"center\"\u003e\n  🚨 Fail with an \u003ccode\u003eError\u003c/code\u003e object and a conventional \u003ccode\u003ecode\u003c/code\u003e property.\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/@studio/fail\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/@studio/fail.svg\" alt=\"npm Version\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://semver.org\"\u003e\n    \u003cimg src=\"https://img.shields.io/:semver-%E2%9C%93-blue.svg\" alt=\"SemVer\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/javascript-studio/fail/actions\"\u003e\n    \u003cimg src=\"https://github.com/javascript-studio/studio-fail/workflows/Build/badge.svg\" alt=\"Build Status\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://opensource.org/licenses/MIT\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/License-MIT-brightgreen.svg\" alt=\"License\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Usage (async / await)\n\n```js\nconst { failure, INVALID } = require('@studio/fail');\n\nfunction read(filename, callback) {\n  if (!filename) {\n    // Easily fail with a conventional error:\n    throw failure('Missing filename', INVALID);\n  }\n\n  // ...\n}\n```\n\n## Usage (callback)\n\n```js\nconst fs = require('fs');\nconst { fail, then, INVALID } = require('@studio/fail');\n\nfunction read(filename, callback) {\n  if (!filename) {\n    // Easily fail with a conventional error:\n    fail(callback, 'Missing filename', INVALID);\n    return;\n  }\n\n  // Wrap callbacks with and error handling, guarding from multiple invocations:\n  fs.readFile(\n    filename,\n    'utf8',\n    then(callback, (content) =\u003e {\n      // Non-undefined return value is passed to callback:\n      return content.trim();\n    })\n  );\n}\n```\n\n## Conventions\n\nErrors should always have a `code` property with an uppercase error code. To\nsimplify error handling, use the provided `fail` utility function and code\nconstants.\n\nError codes follow these conventions:\n\n- Fatal errors should have an error code starting with `E_`. This is the\n  asynchronous equivalent to `throw`. The provided `message` is not supposed to\n  be shown to the user.\n- Other error codes have no prefix and are not considered fatal, for example a\n  validation error. The provided `message` may be shown to the user.\n- If no `code` property is provided, it defaults to `E_FAILED`.\n\nThe provided error codes can be handled generically. You may define additional\nerror codes as needed.\n\n## Install\n\n```bash\n❯ npm i @studio/fail\n```\n\n## API\n\n- `failure(message[, cause][, code[, properties]])`: Create an `Error`\n  with the given message and `cause` and `code` and `properties`. If no `code`\n  is provided it defaults to `E_FAILED`. The `cause` must be an error object.\n- `fail(callback, message[, cause][, code[, properties]])`: Creates a failure\n  and invoked the given `callback` with it.\n- `isFatal(error)`: Whether the given error has a `code` property the starts\n  with `E_` or has no `code` property.\n- `then(callback, next)`: Create a callback function that invokes\n  `callback(err)` if an error occurred and `next(result)` on success. Throws if\n  the function is invoked more than once with error code `E_FAILED` and `err`\n  as the cause. If `next` returns a non-undefined value, the `callback` is\n  invoked with that result.\n\n### Error codes\n\n- `E_FAILED`: Fatal error.\n- `INVALID`: Invalid or missing argument or parameter.\n- `FORBIDDEN`: User is not allowed to access.\n- `NOT_FOUND`: Resource does not exist.\n\n## Examples\n\nThrowing errors:\n\n```js\nconst { failure, INVALID } = require('@studio/fail');\n\n// Fail with a message:\nthrow failure('Oups!');\n\n// The previous is the same as this:\nthrow failure('Oups!', E_FAILED);\n\n// Fail with `code` INVALID:\nthrow failure('Oups!', INVALID);\n\n// Fail with a `cause`:\nconst cause = new Error();\nthrow failure('Oups!', cause);\n\n// Fail with a `cause` and `code` INVALID:\nthrow failure('Oups!', cause, INVALID);\n\n// Fail with `properties` and `code` INVALID:\nthrow failure('Oups!', INVALID, { some: 42 });\n```\n\nInvoking callbacks with errors:\n\n```js\nconst { fail, FORBIDDEN } = require('@studio/fail');\n\nfail(callback, 'Oups!', FORBIDDEN);\n```\n\n## Related modules\n\n- 👻 [Studio Log][1] is a tiny ndjson logger that is `code` and `cause` aware.\n- 📦 [Studio Changes][2] is used to create the changelog for this module.\n\n## License\n\nMIT\n\n\u003cp align=\"center\"\u003eMade with ❤️ on 🌍\u003cp\u003e\n\n[1]: https://github.com/javascript-studio/studio-log\n[2]: https://github.com/javascript-studio/studio-changes\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavascript-studio%2Ffail","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjavascript-studio%2Ffail","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavascript-studio%2Ffail/lists"}