{"id":21961961,"url":"https://github.com/givensuman/tryvial","last_synced_at":"2025-03-22T20:29:15.209Z","repository":{"id":148256917,"uuid":"618486891","full_name":"givensuman/tryvial","owner":"givensuman","description":"🧪 value-packed alternative to try/catch hell","archived":false,"fork":false,"pushed_at":"2023-03-27T15:16:00.000Z","size":52,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-28T23:10:41.982Z","etag":null,"topics":["catch","error-handling","try","try-catch"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/tryvial","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/givensuman.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}},"created_at":"2023-03-24T15:16:45.000Z","updated_at":"2024-10-01T08:00:06.000Z","dependencies_parsed_at":"2023-05-19T13:30:39.314Z","dependency_job_id":null,"html_url":"https://github.com/givensuman/tryvial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givensuman%2Ftryvial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givensuman%2Ftryvial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givensuman%2Ftryvial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givensuman%2Ftryvial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/givensuman","download_url":"https://codeload.github.com/givensuman/tryvial/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245018806,"owners_count":20548080,"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":["catch","error-handling","try","try-catch"],"created_at":"2024-11-29T10:30:18.171Z","updated_at":"2025-03-22T20:29:15.183Z","avatar_url":"https://github.com/givensuman.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `tryvial`\n### Value-packed alternative to try/catch hell \n\nPronounced like \"trivial\" (trĭv′ē-əl)\n\nThis is a utility library that simplifies the try/catch block and packs on functionality for handling asynchronous operations.\n\n## Installation\n\nYou can install the package using npm:\n\n```bash\nnpm install tryvial\n# or\nyarn add tryvial\n# or \npnpm i tryvial\n```\n## Usage\n\n`tryvial` tries to simplify code by wrapping the provided function in a try/catch block. If an error is thrown, you can provide an optional fallback(s) or a handler to manage the error. If you enable the retry functionality, the package will attempt to retry the operation a specified number of times. It's also got timeout support, fallbacks, the works.\n\nThat's the gist of it, here's the implementation:\n\n```js\nimport t from \"tryvial\";\n\nconst do_something = async () =\u003e {\n  // your asynchronous code here\n};\n\nconst handle_an_error = async (error?: Error) =\u003e {\n  // your error handling here\n}\n\n// Simple implementation\nconst result = await t(do_something, {\n  onError: handle_an_error\n})\n\n// Kitchen sink\nconst result = await t(do_something, {\n  retry: true,\n  retries: 5,\n  retryDelay: 100,\n  jitter: 500,\n  fallback: [...some_fallbacks],\n  timeout: true,\n  timeoutAfter: 9999,\n  onTimeout: () =\u003e console.error(\"A timeout occured\"),\n  onRetry: retry =\u003e console.log(`Retrying... ${retry}/5`),\n  onSuccess: result =\u003e console.log(`Success: ${result}`),\n  onError: handle_an_error\n})\n```\n\n## Parameters\n\n`tryvial` only has two parameters, the `fn` that it tries, and the `options` it applies:\n\n|name|type|default|description|\n|---|---|---|---|\n|retry|`boolean`|`false`|Whether or not to retry `fn`|\n|retries|`number`|`2`|Number of times to retry before moving to the catch block|\n|retryDelay|`number`|`0`|Delay (ms) to be implemented between retries|\n|jitter|`number`|`0`|Delay (ms) to be randomly added to retries to prevent network bottlenecking with batch requests. For example, a `jitter` of 500 will add an additional delay anywhere between 0ms and 500ms.|\n|fallback|`(() =\u003e Promise\u003cT\u003e) \\| Array\u003c() =\u003e Promise\u003cT\u003e\u003e`|`undefined`|Fallback function or array of fallback functions to attempt if `fn` fails. Does not attempt retries. If an array is passed and all fallbacks fail, `onError` is passed an array of errors (`Error[]`).|\n|timeout|`boolean`|`false`|Whether or not to enforce a timeout, after which `fn` automatically fails|\n|timeoutAfter|`number`|`10000`|Time (ms) after which `fn` will automatically fail|\n|onTimeout|`() =\u003e void`|`undefined`|Callback to run if timeout happens|\n|onRetry|`(retryNumber: number) =\u003e void`|`undefined`|Callback to run if retry happens. Passes the current retry number as argument.|\n|onSuccess|`(result: T) =\u003e void`|`undefined`|Callback to run if `fn` or `fallback` succeeds. Passes `result as T` as argument.|\n|onError|`(error: Error \\| Error[]) =\u003e void`|`undefined`|Callback to run if `fn` or `fallback` fails. Passes `error as Error` or `error as Error[]` as argument.|\n\n## Contributing\n\nPull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgivensuman%2Ftryvial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgivensuman%2Ftryvial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgivensuman%2Ftryvial/lists"}