{"_id":"@arukin/better-try-catch","_rev":"2-e33f37c5e06e6b4769dd06f739032821","name":"@arukin/better-try-catch","dist-tags":{"latest":"1.1.0"},"versions":{"1.0.0":{"name":"@arukin/better-try-catch","version":"1.0.0","_id":"@arukin/better-try-catch@1.0.0","maintainers":[{"name":"alkinguler","email":"taneralkinguler@gmail.com"}],"homepage":"https://github.com/alkinguler/better-try-catch#readme","bugs":{"url":"https://github.com/alkinguler/better-try-catch/issues"},"dist":{"shasum":"28e7f6cf2f82a46a40edfae5145d80c75edcb257","tarball":"https://registry.npmjs.org/@arukin/better-try-catch/-/better-try-catch-1.0.0.tgz","fileCount":7,"integrity":"sha512-meGX7dmlRebOZWchp/QZ4L1h3dvSe/Mi9XGn4551LclSXmrWj27QGHpvleepdVBhhtHwXUsuB+Jf2BKVh1rUgQ==","signatures":[{"sig":"MEQCIHbIgYKimq4qJmqS6lS+FZkkKj+o2QcduWBPCI44cP6jAiAai6SUVghijgKBUfcmroGF/zJWtspqmyeDtwpR6f0/wQ==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":9519},"main":"./dist/index.cjs","type":"module","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js","require":"./dist/index.cjs"}},"gitHead":"f3ce6a73805d11fa93230f37c3fcf9fb2b7ef802","scripts":{"build":"tsup index.ts --format cjs,esm --dts --clean","prepublishOnly":"bun run build"},"_npmUser":{"name":"alkinguler","email":"taneralkinguler@gmail.com"},"repository":{"url":"git+https://github.com/alkinguler/better-try-catch.git","type":"git"},"_npmVersion":"10.8.2","description":"A better try-catch wrapper for TypeScript and JavaScript.","directories":{},"_nodeVersion":"20.20.2","_hasShrinkwrap":false,"devDependencies":{"tsup":"^8.5.1","@types/bun":"latest","@biomejs/biome":"2.4.15"},"peerDependencies":{"typescript":"^5"},"_npmOperationalInternal":{"tmp":"tmp/better-try-catch_1.0.0_1778413386880_0.3027595561839789","host":"s3://npm-registry-packages-npm-production"}},"1.1.0":{"name":"@arukin/better-try-catch","version":"1.1.0","description":"A better try-catch wrapper for TypeScript and JavaScript.","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js","require":"./dist/index.cjs"}},"scripts":{"build":"tsup index.ts --format cjs,esm --dts --clean","prepublishOnly":"bun run build"},"type":"module","devDependencies":{"@biomejs/biome":"2.4.15","@types/bun":"latest","tsup":"^8.5.1"},"peerDependencies":{"typescript":"^5"},"repository":{"type":"git","url":"git+https://github.com/alkinguler/better-try-catch.git"},"_id":"@arukin/better-try-catch@1.1.0","gitHead":"6fb51e68ab80f6986a78f32173a39c4226bd0310","bugs":{"url":"https://github.com/alkinguler/better-try-catch/issues"},"homepage":"https://github.com/alkinguler/better-try-catch#readme","_nodeVersion":"20.20.2","_npmVersion":"10.8.2","dist":{"integrity":"sha512-tvEHDtJrTu1dae0PhVXawVrHr/d9m/+yduFhdg1ZWFBZIe/yYtjZsIogHdKp2L3dafiGEd+HVnO35J0T2c4+Kg==","shasum":"5a9810fe87cf3e091ab5644b4319a29b2fa6f6e1","tarball":"https://registry.npmjs.org/@arukin/better-try-catch/-/better-try-catch-1.1.0.tgz","fileCount":7,"unpackedSize":9491,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCMSIw5p1i5Dt/QQ4r9wEkOVEy8YZA+2WKltIjlQh+CbgIhAPG3qh/1e2AjL9hB31EX/bh/YRJtc4meW+5jYRU8L9vh"}]},"_npmUser":{"name":"alkinguler","email":"taneralkinguler@gmail.com"},"directories":{},"maintainers":[{"name":"alkinguler","email":"taneralkinguler@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/better-try-catch_1.1.0_1778413988562_0.46918567358322893"},"_hasShrinkwrap":false}},"time":{"created":"2026-05-10T11:43:06.803Z","modified":"2026-05-10T11:53:08.821Z","1.0.0":"2026-05-10T11:43:07.026Z","1.1.0":"2026-05-10T11:53:08.697Z"},"bugs":{"url":"https://github.com/alkinguler/better-try-catch/issues"},"homepage":"https://github.com/alkinguler/better-try-catch#readme","repository":{"type":"git","url":"git+https://github.com/alkinguler/better-try-catch.git"},"description":"A better try-catch wrapper for TypeScript and JavaScript.","maintainers":[{"name":"alkinguler","email":"taneralkinguler@gmail.com"}],"readme":"# BetterTryCatch\n\nA better, type-safe try-catch wrapper for TypeScript and JavaScript. It eliminates the need for block-scoped `let` variables and deeply nested `try/catch` blocks by returning a predictable `Result` object.\n\n## Installation\n\nYou can install this package using your favorite package manager:\n\n```bash\n# npm\nnpm install @arukin/better-try-catch\n\n# bun\nbun add @arukin/better-try-catch\n\n# yarn\nyarn add @arukin/better-try-catch\n\n# pnpm\npnpm add @arukin/better-try-catch\n```\n\n## Usage\n\n### Asynchronous Operations (`tryCatch`)\n\nWrap your promises to safely handle errors without `try/catch` blocks.\n\n```typescript\nimport { tryCatch } from '@arukin/better-try-catch';\n\nasync function fetchUser(userId: string) {\n\t// Notice how we don't need a try/catch block!\n\tconst { data, error } = await tryCatch(fetch(`/api/users/${userId}`));\n\n\tif (error) {\n\t\tconsole.error(\"Failed to fetch user:\", error.message);\n\t\treturn;\n\t}\n\n\t// data is fully typed based on the Promise's return type!\n\tconsole.log(\"User data:\", data);\n}\n```\n\n### Synchronous Operations (`tryCatchSync`)\n\nUse it for synchronous code that might throw exceptions, like `JSON.parse`.\n\n```typescript\nimport { tryCatchSync } from '@arukin/better-try-catch';\n\nfunction parseConfig(jsonString: string) {\n\tconst { data, error } = tryCatchSync(() => JSON.parse(jsonString));\n\n\tif (error) {\n\t\tconsole.error(\"Invalid JSON!\");\n\t\treturn null;\n\t}\n\n\treturn data;\n}\n```\n\n### Type Guards\n\nWe provide handy `isSuccess` and `isFailure` type guards to safely narrow down the `Result` type.\n\n```typescript\nimport { tryCatch, isSuccess, isFailure } from '@arukin/better-try-catch';\n\nasync function processData() {\n\tconst result = await tryCatch(someAsyncOperation());\n\n\tif (isFailure(result)) {\n\t\t// result.error is now narrowed to the Error type\n\t\t// result.data is null\n\t\tconsole.log(result.error);\n\t\treturn;\n\t}\n\n\tif (isSuccess(result)) {\n\t\t// result.data is narrowed to the expected type\n\t\t// result.error is null\n\t\tconsole.log(result.data);\n\t}\n}\n```\n\n## API Reference\n\n### `tryCatch<T, E extends Error = Error>(promise: Promise<T>): Promise<Result<T, E>>`\nExecutes an asynchronous promise and returns a `Result` object containing either `data` or `error`.\n\n### `tryCatchSync<T, E extends Error = Error>(fn: () => T): Result<T, E>`\nExecutes a synchronous function and returns a `Result` object.\n\n### `isSuccess<T, E>(result: Result<T, E>): result is Success<T>`\nType guard to check if a result was successful. Narrows the type to `Success<T>`.\n\n### `isFailure<T, E>(result: Result<T, E>): result is Failure<E>`\nType guard to check if a result failed. Narrows the type to `Failure<E>`.\n\n## License\n\nMIT\n","readmeFilename":"README.md"}