{"id":15407934,"url":"https://github.com/syhner/betterr","last_synced_at":"2025-07-14T17:15:30.274Z","repository":{"id":136778269,"uuid":"611958395","full_name":"syhner/betterr","owner":"syhner","description":"A better way to handle errors. Both data and errors are declared with const, available at the top level, and non-nullable (once the other is handled). Errors are always Error objects.","archived":false,"fork":false,"pushed_at":"2024-09-05T00:46:57.000Z","size":170,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-22T05:19:25.415Z","etag":null,"topics":["async-await","error-handling","errors","exceptions","package","promise","try-catch","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/betterr","language":"TypeScript","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/syhner.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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":"2023-03-09T22:32:05.000Z","updated_at":"2024-09-12T06:12:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"8aced0d0-014d-42bc-a2ec-1aa3ad6a4caa","html_url":"https://github.com/syhner/betterr","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syhner%2Fbetterr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syhner%2Fbetterr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syhner%2Fbetterr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syhner%2Fbetterr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syhner","download_url":"https://codeload.github.com/syhner/betterr/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241377881,"owners_count":19953182,"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":["async-await","error-handling","errors","exceptions","package","promise","try-catch","typescript"],"created_at":"2024-10-01T16:30:15.995Z","updated_at":"2025-03-01T14:31:06.807Z","avatar_url":"https://github.com/syhner.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# betterr\n\nA better way to handle errors\n\n[![build status](https://img.shields.io/github/actions/workflow/status/syhner/betterr/CD.yml)](https://github.com/Syhner/betterr/actions/workflows/CD.yml)\n[![coverage](https://img.shields.io/badge/dynamic/json?color=brightgreen\u0026label=coverage\u0026query=%24.total.lines.pct\u0026suffix=%25\u0026url=https%3A%2F%2Fraw.githubusercontent.com%2FSyhner%2Fbetterr%2Fcoverage%2Fcoverage-summary.json)](https://syhner.github.io/betterr/)\n\n## Advantages\n\nUnlike with try...catch or promises:\n\n- Both data and errors are declared with const, available at the top level, and non-nullable (once the other is handled)\n- Errors are always Error objects\n\n## Installation\n\n```sh\n$ npm install betterr\n```\n\n## Usage\n\n1. Wrap any code that may throw\n\n```ts\nimport { betterr } from 'betterr'; // const { betterr } = require('betterr');\n\nconst [user, err] = await betterr(() =\u003e getUserWithId(1));\n// user: User | null, err: Error | null\n```\n\n2. Now either\n\n- Avoid handling the error, and use optional chaining\n\n  ```ts\n  const maybeName = user?.name; // maybeName: string | undefined\n  ```\n\n- Handle the error (interrupting the control flow), after which optional chaining is not needed\n\n  ```ts\n  if (err) return; // user: User (after error handled)\n  const name = user.name; // name: string\n  ```\n\n## Explanation\n\n`betterr` / `betterSync` execute a callback and return a tuple with `data` (callback return value) and `err` (error during execution), one of which will be null depending on the success of the callback.\n\n- `betterr` can be used with both asynchronous and synchronous callbacks.\n\n- `betterrSync` can only be used with synchronous callbacks, but avoids wrapping the data in a promise so that `await` is not necessary.\n\n## TypeScript\n\nBoth `betterr` and `betterrSync` are generic.\n\n- The callback return type must be assignable to the first generic parameter (for `data`). It defaults to the callback return type.\n- The second generic parameter (for `err`) must extend the `Error` object. It defaults to `Error`.\n\n```ts\n/**\n * const betterrSync: \u003cTData, TError extends Error = Error\u003e\n * (callback: () =\u003e TData) =\u003e [TData, null] | [null, TError]\n */\n\nconst [user, err] = betterrSync\u003cUser, RangeError\u003e(() =\u003e ({ id: 1 }));\n// data: User | null, err: RangeError | null\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyhner%2Fbetterr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyhner%2Fbetterr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyhner%2Fbetterr/lists"}