{"id":31940354,"url":"https://github.com/dsnchz/try-catch","last_synced_at":"2025-10-14T08:53:36.964Z","repository":{"id":292221782,"uuid":"980138945","full_name":"dsnchz/try-catch","owner":"dsnchz","description":"Simple try-catch utility function for JavaScript","archived":false,"fork":false,"pushed_at":"2025-06-05T17:14:16.000Z","size":85,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-18T11:04:32.333Z","etag":null,"topics":["try-catch","try-catch-wrapper","utility-function","utility-library"],"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/dsnchz.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":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-08T16:16:37.000Z","updated_at":"2025-06-15T16:55:07.000Z","dependencies_parsed_at":"2025-05-08T19:29:39.072Z","dependency_job_id":"9a770a27-f65d-4cd5-988a-d3f385bc5bd1","html_url":"https://github.com/dsnchz/try-catch","commit_stats":null,"previous_names":["dsnchz/try-catch"],"tags_count":1,"template":false,"template_full_name":"thedanchez/template-solidjs-library","purl":"pkg:github/dsnchz/try-catch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsnchz%2Ftry-catch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsnchz%2Ftry-catch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsnchz%2Ftry-catch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsnchz%2Ftry-catch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dsnchz","download_url":"https://codeload.github.com/dsnchz/try-catch/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsnchz%2Ftry-catch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279018312,"owners_count":26086348,"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-14T02:00:06.444Z","response_time":60,"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":["try-catch","try-catch-wrapper","utility-function","utility-library"],"created_at":"2025-10-14T08:53:00.785Z","updated_at":"2025-10-14T08:53:36.958Z","avatar_url":"https://github.com/dsnchz.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @dschz/try-catch\n\n[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)\n[![npm](https://img.shields.io/npm/v/@dschz/try-catch?color=blue)](https://www.npmjs.com/package/@dschz/try-catch)\n[![Bundle Size](https://img.shields.io/bundlephobia/minzip/@dschz/try-catch)](https://bundlephobia.com/package/@dschz/try-catch)\n[![JSR](https://jsr.io/badges/@dschz/try-catch/score)](https://jsr.io/@dschz/try-catch)\n[![CI](https://github.com/dsnchz/try-catch/actions/workflows/ci.yaml/badge.svg)](https://github.com/dsnchz/try-catch/actions/workflows/ci.yaml)\n\n\u003e A tiny utility to wrap promises or async functions and return a `[error, data]` tuple — no more `try/catch` boilerplate.\n\n## ✨ Features\n\n- ✅ Supports both async functions and raw promises\n- ✅ Catches both **sync and async** errors\n- ✅ Strongly typed result via `Result\u003cT, E\u003e`\n- ✅ Zero dependencies — just TypeScript\n\n## 📆 Installation\n\n```bash\nnpm install @dschz/try-catch\npnpm install @dschz/try-catch\nyarn install @dschz/try-catch\nbun install @dschz/try-catch\n```\n\n## 🚀 Usage\n\n### Wrapping a promise result\n\n```ts\nimport { tryCatch } from \"@dschz/try-catch\";\n\nconst [err, res] = await tryCatch(fetch(\"/api/data\"));\n```\n\n### Wrapping an async function\n\n```ts\nconst [err, user] = await tryCatch(() =\u003e fetchUserById(123));\n```\n\n### Wrapping a sync function that might throw\n\n```ts\nconst [err, parsed] = await tryCatch(() =\u003e JSON.parse('{\"valid\":true}'));\n\nif (err) {\n  console.error(\"Invalid JSON:\", err.message);\n}\n```\n\n## Note\n\n⚠️ Always wrap expressions that might throw in a function.\nThis ensures the error is caught inside the try-catch scope.\n\n```ts\n// ✅ CORRECT\nawait tryCatch(() =\u003e JSON.parse(\"{ malformed }\"));\n\n// ❌ INCORRECT — throws before tryCatch is even called\nawait tryCatch(JSON.parse(\"{ malformed }\"));\n```\n\n## 🧠 Types\n\n```ts\ntype Success\u003cT\u003e = [error: null, data: T];\ntype Failure\u003cE extends Error = Error\u003e = [error: E, data: null];\ntype Result\u003cT, E extends Error = Error\u003e = Success\u003cT\u003e | Failure\u003cE\u003e;\n```\n\nThe return value is a tuple:\n\n```ts\n[error, data]; // One will always be null\n```\n\n## 🧪 Example with Custom Error Types\n\n```ts\nclass MyError extends Error {\n  constructor(message: string) {\n    super(message);\n    this.name = \"MyError\";\n  }\n}\n\nconst [err, data] = await tryCatch\u003cMyType, MyError\u003e(() =\u003e doSomething());\n```\n\n## 📄 License\n\nMIT © [Daniel Sanchez](https://github.com/thedanchez)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsnchz%2Ftry-catch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdsnchz%2Ftry-catch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsnchz%2Ftry-catch/lists"}