{"id":31384527,"url":"https://github.com/itsezz/try-catch","last_synced_at":"2026-02-07T10:06:10.764Z","repository":{"id":297487464,"uuid":"987207350","full_name":"itsEzz/try-catch","owner":"itsEzz","description":"A lightweight TypeScript utility that transforms error handling with type-safe Result patterns, making your code cleaner and more predictable.","archived":false,"fork":false,"pushed_at":"2026-02-02T06:34:09.000Z","size":431,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-02-02T18:30:43.403Z","etag":null,"topics":["error-handling","result-pattern","try-catch","typescript","utility"],"latest_commit_sha":null,"homepage":"","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/itsEzz.png","metadata":{"files":{"readme":"README.md","changelog":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-05-20T18:25:59.000Z","updated_at":"2026-02-02T06:34:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"9578444f-0cb7-4f44-a1d0-c5aa8c38293c","html_url":"https://github.com/itsEzz/try-catch","commit_stats":null,"previous_names":["itsezz/try-catch"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/itsEzz/try-catch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itsEzz%2Ftry-catch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itsEzz%2Ftry-catch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itsEzz%2Ftry-catch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itsEzz%2Ftry-catch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/itsEzz","download_url":"https://codeload.github.com/itsEzz/try-catch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itsEzz%2Ftry-catch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29192004,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T07:37:03.739Z","status":"ssl_error","status_checked_at":"2026-02-07T07:37:03.029Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["error-handling","result-pattern","try-catch","typescript","utility"],"created_at":"2025-09-28T15:13:02.366Z","updated_at":"2026-02-07T10:06:10.758Z","avatar_url":"https://github.com/itsEzz.png","language":"TypeScript","readme":"# @itsezz/try-catch\n\n[![npm version](https://img.shields.io/npm/v/@itsezz/try-catch.svg)](https://npmjs.com/package/@itsezz/try-catch)\n\nType-safe error handling for TypeScript. No more `try/catch` blocks.\n\n## Install\n\n```bash\nnpm install @itsezz/try-catch\n```\n\n## Result Types\n\n```typescript\ntype Result\u003cT, E = Error\u003e = Success\u003cT\u003e | Failure\u003cE\u003e;\n\ntype Success\u003cT\u003e = { data: T; error?: never; ok: true };\ntype Failure\u003cE\u003e = { data?: never; error: E; ok: false };\n```\n\nUse `result.ok` to check success/failure with full TypeScript narrowing.\n\n## Quick Start\n\n```typescript\nimport { tryCatch, isError } from '@itsezz/try-catch';\n\n// Sync\nconst result = tryCatch(() =\u003e JSON.parse('{\"name\":\"user\"}'));\n\nif (result.ok) console.log(result.data.name); // \"user\"\nelse console.error(result.error);\n\n// Async\nconst user = await tryCatch(fetch('/api/user').then(r =\u003e r.json()));\n\nif (isError(user)) return null;\nreturn user.data;\n```\n\n## API\n\n### Functions\n\n| Function          | Short alias | Returns                     | Description             |\n| ----------------- | ----------- | --------------------------- | ----------------------- |\n| `tryCatch()`      | `t()`       | `Result \\| Promise\u003cResult\u003e` | Auto-detects sync/async |\n| `tryCatchSync()`  | `tc()`      | `Result`                    | Guaranteed sync         |\n| `tryCatchAsync()` | `tca()`     | `Promise\u003cResult\u003e`           | Guaranteed async        |\n\n\u003e **Note**: `tryCatch` may not correctly infer if the result is `Promise\u003cResult\u003cT,E\u003e\u003e` or `Result\u003cT,E\u003e` in certain conditions and defaults to `Promise\u003cResult\u003cT,E\u003e\u003e` when unsure. Use explicit variants for guaranteed type safety.\n\n### Create Results\n\n```typescript\nsuccess(42)     // { data: 42, ok: true }\nfailure('err')  // { error: 'err', ok: false }\n```\n\n### Check Results\n\n```typescript\nif (result.ok) result.data; // Success\u003cT\u003e\nelse result.error;          // Failure\u003cE\u003e\n\nisSuccess(result);  // type guard\nisError(result);    // type guard\n```\n\n### Transform Results\n\n```typescript\nmap(result, fn)             // transform success value\nflatMap(result, fn)         // chain operations returning Result\nunwrapOr(result, default)   // get value or default\nunwrapOrElse(result, fn)    // get value or compute from error\nmatch(result, {             // pattern matching\n  success: (data) =\u003e ...,\n  failure: (error) =\u003e ...\n})\n```\n\n## Examples\n\n### Safe JSON Parsing\n\n```typescript\nconst json = tryCatch(() =\u003e JSON.parse(input));\n\nif (!json.ok) return { error: 'Invalid JSON' };\nreturn json.data;\n```\n\n### API Request with Type Safety\n\n```typescript\ninterface User {\n  id: number;\n  name: string;\n}\n\nconst fetchUser = async (): Promise\u003cUser | null\u003e =\u003e {\n  const result = await tryCatch(fetch('/api/user').then(r =\u003e r.json()));\n  if (result.ok) return result.data;\n  return null;\n};\n```\n\n### Validation\n\n```typescript\nconst validate = (input: unknown): Result\u003cUser, string[]\u003e =\u003e {\n  const parsed = tryCatch(() =\u003e JSON.parse(input as string));\n  if (!parsed.ok) return failure(['Invalid JSON']);\n\n  const errors: string[] = [];\n  if (!parsed.data.name) errors.push('Name required');\n\n  return errors.length \u003e 0 ? failure(errors) : success(parsed.data);\n};\n```\n\n### Functional Pipeline\n\n```typescript\nconst result = unwrapOr(\n  flatMap(success('42'), s =\u003e success(parseInt(s))),\n  0\n); // 84\n```\n\n## FAQ\n\n**Default error type?** `Error`. Use generics for custom types:\n```typescript\ntryCatch\u003cnumber, ApiError\u003e(() =\u003e fetchData())\n```\n\n**When to use each variant?**\n- `tryCatch`: Don't care about sync/async distinction\n- `tryCatchSync`: Need guaranteed sync return\n- `tryCatchAsync`: Need guaranteed `Promise\u003cResult\u003e` return\n\n## License\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitsezz%2Ftry-catch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fitsezz%2Ftry-catch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitsezz%2Ftry-catch/lists"}