{"id":25565968,"url":"https://github.com/thadeu/safetry","last_synced_at":"2026-03-05T22:06:04.430Z","repository":{"id":276408087,"uuid":"929196966","full_name":"thadeu/safetry","owner":"thadeu","description":"A simple and lightweight transform to create a safe try/catch expressions in JavaScript/Typescript","archived":false,"fork":false,"pushed_at":"2025-04-30T19:21:33.000Z","size":114,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-25T00:35:53.116Z","etag":null,"topics":["javascript","try-catch"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/thadeu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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}},"created_at":"2025-02-08T01:50:57.000Z","updated_at":"2025-04-12T14:56:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"2b9d7411-4f09-4ac5-a50d-26591fb4ac59","html_url":"https://github.com/thadeu/safetry","commit_stats":null,"previous_names":["thadeu/safetry"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thadeu/safetry","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thadeu%2Fsafetry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thadeu%2Fsafetry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thadeu%2Fsafetry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thadeu%2Fsafetry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thadeu","download_url":"https://codeload.github.com/thadeu/safetry/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thadeu%2Fsafetry/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266870389,"owners_count":23998246,"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-07-24T02:00:09.469Z","response_time":99,"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":["javascript","try-catch"],"created_at":"2025-02-20T22:21:27.226Z","updated_at":"2026-03-05T22:05:59.392Z","avatar_url":"https://github.com/thadeu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ch1 align=\"center\"\u003e🛟 safetry\u003c/h1\u003e\n  \u003cp align=\"center\"\u003e\u003ci\u003eA simple and lightweight transform to create a safe try/catch expressions in JavaScript/Typescript\u003c/i\u003e\u003c/p\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg alt=\"Build Status\" src=\"https://github.com/thadeu/safetry/actions/workflows/ci.yml/badge.svg?cacheSeconds=0\"\u003e  \n  \u003cimg alt=\"GitHub package.json version (subfolder of monorepo)\" src=\"https://img.shields.io/github/package-json/v/thadeu/safetry?cacheSeconds=0\"\u003e\n\u003c/p\u003e\n\n\u003e [!WARNING]\n\u003e THIS IS AN EXPERIMENTAL PLUGIN TO USE WITH ESBUILD, WE DONT ENCOURAGE TO USE IT IN PRODUCTION YET.\n\n## Motivation\n\nBecause in sometimes, we need to use safe conditions with try/catch easyless. But without multiple line or blocks.\n\nA few days we saw an idea to this, `Safe Assignment Operator Proposal`. But, maybe it wont be able to advanced to next step in the tc39 proposals.\n\nhttps://github.com/arthurfiorette/proposal-try-operator/tree/proposal-safe-assignment-operator\n\nAlso, the same author created another proposal for try/catch, called `Try Operator`\n\nhttps://github.com/arthurfiorette/proposal-try-operator\n\n\u003cbr /\u003e\n\n\u003e [!NOTE]\n\u003e I decided to create a plugin to ESBuild to make my first idea to this implementation, using IIFE and Transform code.\n\n## Documentation \u003c!-- omit in toc --\u003e\n\nVersion    | Documentation\n---------- | -------------\nunreleased | https://github.com/thadeu/safetry/README.md\n\n## Table of Contents \u003c!-- omit in toc --\u003e\n  - [Installation](#installation)\n  - [Compatibility](#Ccmpatibility)\n  - [Configuration](#configuration)\n  - [Usage](#usage)\n\n## Compatibility\n\n| kind           | branch  | javascript         |\n| -------------- | ------- | ------------------ |\n| unreleased     | main    | \u003e= 20.x |\n\n## Installation\n\nUse Yarn\n\n```bash\nyarn add @thadeu/safetry\n```\n\nor use NPM\n\n```bash\nnpm i @thadeu/safetry\n```\n\nor use PNPM\n\n```bash\npnpm add @thadeu/safetry\n```\n\nor use Github directly\n\n```bash\npnpm add github:@thadeu/safetry\n```\n\nand then, enjoy!\n\n## Configuration\n\nCurrently, we support only ESBuild to compile safetry expressions.\n\n```ts\nimport { build } from 'esbuild';\nimport { safetryPlugin } from '@thadeu/safetry';\n\nawait esbuild.build({\n  ...\n  plugins: [safetryPlugin],\n});\n```\n\n## Usage\n\nYou dont need import anything to use safetry, because we going to transform your code in the build step.\n\nFor example:\n\n**Imagine that you could something like this:**\n\n```ts\nconst [error, value] = safetry { 2 + 2 }\n```\n\nThat's mean that if the expression throws an error, the variable `error` will be set with the error object and `value` will be `undefined`.\n\nAlso, the last expression always will be returned to the value. You can use `return` or not, like Ruby, Rust and another languages.\n\nLet's see an others examples:\n\n```ts\nasync function calc() {\n  const [err, value] = await safetry {\n    const x = 10;\n    const y = 20;\n\n    x + y\n  }\n\n  if (err) {\n    return err\n  }\n\n  return value\n}\n```\n\nInside a function, you can use `safetry` like a normal function.\n\n```ts\nfunction safeOperation(operation) {\n  return safetry {\n    return operation()\n  }\n}\n\nconst [err4, val4] = safeOperation(() =\u003e 42)\n```\n\nPerforming array operations\n\n```ts\nconst [error5, value5] = safetry {\n  const arr = [1, 2, 3];\n\n  return arr.map(x =\u003e x * 2);\n}\n```\n\nYou can create a complex calcs inside the block and then return you any value.\n\n```ts\nconst [error, value] = safetry {\n  const numbers = [1, 2, 3, 4, 5];\n  const sum = numbers.reduce((acc, curr) =\u003e acc + curr, 0);\n  const average = sum / numbers.length;\n\n  return {\n    sum,\n    average,\n    squared: sum * average\n  }\n}\n```\n\n```ts\nfunction riskyFunction(shouldThrow) {\n  if (shouldThrow) throw new Error('Risky business!');\n\n  return 'Ok!';\n}\n\nconst [error, value] = safetry { riskyFunction(false) }\n```\n\nAlso we support to use async functions.\n\n```ts\nasync function fetchAsync(): Promise\u003cany\u003e {\n  const [error, response]: AsyncSafetryResult\u003cany\u003e = await safetry {\n    return fetch('https://dummyjson.com/test').then(res =\u003e res.json())\n  }\n\n  return [error, response]\n}\n```\n\n## Development\n\nAfter checking out the repo, install dependencies. Then, run `test` to run the tests.\n\nAlso you must create a input and output files to test your changes with your statements.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/thadeu/safetry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/thadeu/safetry/blob/master/CODE_OF_CONDUCT.md).\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthadeu%2Fsafetry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthadeu%2Fsafetry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthadeu%2Fsafetry/lists"}