{"id":15980943,"url":"https://github.com/vitalets/throw-utils","last_synced_at":"2025-03-17T22:30:58.433Z","repository":{"id":55974797,"uuid":"158733745","full_name":"vitalets/throw-utils","owner":"vitalets","description":"Helpers for error throwing","archived":false,"fork":false,"pushed_at":"2022-10-21T11:15:25.000Z","size":391,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T05:30:41.979Z","etag":null,"topics":["throw","throw-errors"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/vitalets.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":"2018-11-22T17:46:49.000Z","updated_at":"2023-08-18T09:08:08.000Z","dependencies_parsed_at":"2023-01-20T07:15:16.267Z","dependency_job_id":null,"html_url":"https://github.com/vitalets/throw-utils","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitalets%2Fthrow-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitalets%2Fthrow-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitalets%2Fthrow-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitalets%2Fthrow-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vitalets","download_url":"https://codeload.github.com/vitalets/throw-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243887232,"owners_count":20363858,"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":["throw","throw-errors"],"created_at":"2024-10-08T00:23:39.059Z","updated_at":"2025-03-17T22:30:58.099Z","avatar_url":"https://github.com/vitalets.png","language":"TypeScript","readme":"# throw-utils\n\n[![Actions Status](https://github.com/vitalets/throw-utils/workflows/autotests/badge.svg)](https://github.com/vitalets/throw-utils/actions)\n[![npm](https://img.shields.io/npm/v/throw-utils.svg)](https://www.npmjs.com/package/throw-utils)\n[![license](https://img.shields.io/npm/l/throw-utils.svg)](https://www.npmjs.com/package/throw-utils)\n\nTiny helpers for error throwing.\n\n\u003c!-- AUTO-GENERATED-CONTENT:START (TOC) --\u003e\n- [Installation](#installation)\n- [Use Cases](#use-cases)\n- [API](#api)\n  - [throwError](#throwerror)\n  - [throwIf](#throwif)\n  - [toError](#toerror)\n- [License](#license)\n\u003c!-- AUTO-GENERATED-CONTENT:END --\u003e\n\n## Installation\n```bash\nnpm i throw-utils\n```\n\n## Use Cases\n\n1. Assign value or throw error if value is empty:\n   ```diff\n   import { throwError } from 'throw-utils';\n\n   - if (!process.env.FOO) {\n   -   throw new Error('FOO is not defined');\n   - }\n   - const foo = process.env.FOO;\n\n   + const foo = process.env.FOO || throwError('FOO is not defined');\n   ```\n\n2. Return result from function or throw error if result is empty:\n   ```diff\n   import { throwIf } from 'throw-utils';\n\n   function foo(a) {\n   - if (!a) {\n   -   throw new Error('Parameter a is required.');\n   - }\n   - return result;\n\n   + return result || throwError('Empty result');\n   }\n   ```\n\n3. Check function parameters in single line:\n   ```diff\n   import { throwIf } from 'throw-utils';\n\n   function f(a) {\n   - if (!a) {\n   -   throw new Error('Parameter a is required.');\n   - }\n\n   + throwIf(!a, 'Parameter a is required.');\n   }\n   ```\n\n## API\n\u003c!-- AUTO-GENERATED-CONTENT:START (API) --\u003e\n### throwError\n\nThrows new error. Allows simple usage of `throw` in expressions and arrow functions.\n\n| Function | Type |\n| ---------- | ---------- |\n| `throwError` | `(msg: ErrorLike) =\u003e never` |\n\n### throwIf\n\nConditionally throws error. Convenient replacement of `if...throw` block with one-liner:\n\n| Function | Type |\n| ---------- | ---------- |\n| `throwIf` | `(condition: unknown, msg: ErrorLike) =\u003e void` |\n\n### toError\n\nConverts anything to Error.\n\n| Function | Type |\n| ---------- | ---------- |\n| `toError` | `(msg: ErrorLike) =\u003e Error` |\n\u003c!-- AUTO-GENERATED-CONTENT:END --\u003e\n\n## License\nMIT @ [Vitaliy Potapov](https://github.com/vitalets)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitalets%2Fthrow-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvitalets%2Fthrow-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitalets%2Fthrow-utils/lists"}