{"id":20676025,"url":"https://github.com/sammwyy/safetry","last_synced_at":"2025-04-19T20:55:18.144Z","repository":{"id":253836455,"uuid":"844704309","full_name":"sammwyy/safetry","owner":"sammwyy","description":"Safe and structured error handling for JavaScript.","archived":false,"fork":false,"pushed_at":"2024-08-19T20:16:37.000Z","size":59,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T13:22:04.695Z","etag":null,"topics":["error","error-handling","javascript","try-catch","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/safe-try-catch","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/sammwyy.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":"2024-08-19T20:00:42.000Z","updated_at":"2024-08-21T14:45:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"b366699f-b015-45b1-8f0b-99ace52ef9db","html_url":"https://github.com/sammwyy/safetry","commit_stats":null,"previous_names":["sammwyy/safetry"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sammwyy%2Fsafetry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sammwyy%2Fsafetry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sammwyy%2Fsafetry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sammwyy%2Fsafetry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sammwyy","download_url":"https://codeload.github.com/sammwyy/safetry/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249685197,"owners_count":21310564,"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":["error","error-handling","javascript","try-catch","typescript"],"created_at":"2024-11-16T21:11:29.636Z","updated_at":"2025-04-19T20:55:18.139Z","avatar_url":"https://github.com/sammwyy.png","language":"TypeScript","funding_links":["https://ko-fi.com/sammwy"],"categories":[],"sub_categories":[],"readme":"# 🔒 Safe Try Catch\n\nSafe and structured error handling for JavaScript.\n\n## ⚡ Features\n\n- 🔄 Safe error handling for synchronous and asynchronous functions\n- 🔍 Supports `try/catch` for both sync and async operations\n- 🎯 Clear separation between success and failure using `Result\u003cT, E\u003e`\n- 🛠 Compatible with Promises\n- 📦 Easy integration with existing codebases\n- 💡 Inspired by Rust’s error-handling model\n\n## 📦 Install\n\n```bash\n# Using npm\nnpm install safe-try-catch\n\n# Using yarn\nyarn add safe-try-catch\n\n# Using pnpm\npnpm add safe-try-catch\n\n# Using bun\nbun add safe-try-catch\n```\n\n## 📋 Usage\n\n### Basic Example\n\nWraps synchronous or asynchronous functions with safe error handling.\n\n```ts\nimport { safeTry } from 'safe-try-catch';\n\nconst [result, error] = safeTry(() =\u003e {\n  throw new Error(\"This is an error\");\n});\n```\n\n### Wrapping a function\n\nWraps a function to handle errors safely, and can be used with both synchronous and asynchronous functions.\n\n```ts\nimport { wrapTry } from 'safe-try-catch';\n\n// Unsafe synchronous function\nfunction unsafeSyncAction(value: string): string {\n  if (value === \"\") throw new Error(\"Value is empty\");\n  return value.trim();\n}\n\nconst safeAction = wrapTry(unsafeSyncAction);\nconst [result, error] = safeAction(\"  Hello World  \");\n\nif (error) {\n  console.error(error.message);\n} else {\n  console.log(result); // \"Hello World\"\n}\n```\n\n## 📗 API\n\n### `safeTry(fn: () =\u003e T): Result\u003cT, Error\u003e`\n\n- **Description**: Directly executes a function or block of code inside error handling. Ideal for quick, inline code execution.\n- **Parameters**:\n  - `fn`: A synchronous function to be executed.\n- **Returns**: A tuple `[result, error]`.\n\n### `wrapTry\u003cT, E\u003e(fn: (...args: any[]) =\u003e T | Promise\u003cT\u003e): (...args: any[]) =\u003e Promise\u003cResult\u003cT, E\u003e\u003e`\n\n- **Description**: Wraps a given function (synchronous or asynchronous) in safe error handling. Returns a function that handles errors and returns a Promise.\n- **Parameters**:\n  - `fn`: A function to be wrapped that returns a value or a Promise.\n- **Returns**: A function that returns a Promise containing a tuple `[result, error]`.\n\n## 🤝 Contributing\n\nContributions, issues and feature requests are welcome! Feel free to check [issues page](https://github.com/sammwyy/safetry/issues).\n\n## ❤️ Show your support\n\nGive a ⭐️ if this project helped you! Or buy me a coffeelatte 🙌 on [Ko-fi](https://ko-fi.com/sammwy)\n\n## 📝 License\n\nCopyright © 2024 [Sammwy](https://github.com/sammwyy). This project is [MIT](LICENSE) licensed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsammwyy%2Fsafetry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsammwyy%2Fsafetry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsammwyy%2Fsafetry/lists"}