{"id":15354519,"url":"https://github.com/kennethnym/trycat","last_synced_at":"2025-04-15T06:10:55.726Z","repository":{"id":236875381,"uuid":"793321582","full_name":"kennethnym/trycat","owner":"kennethnym","description":"trycat is a lightweight, type-safe, zero-dependency implementation of the Result type","archived":false,"fork":false,"pushed_at":"2024-05-02T22:52:58.000Z","size":95,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T16:51:16.677Z","etag":null,"topics":["error-handling","result","result-type","rust-result"],"latest_commit_sha":null,"homepage":"https://kennethnym.github.io/trycat/","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/kennethnym.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":"2024-04-29T01:59:14.000Z","updated_at":"2024-12-07T11:36:44.000Z","dependencies_parsed_at":"2024-05-01T17:01:10.815Z","dependency_job_id":null,"html_url":"https://github.com/kennethnym/trycat","commit_stats":null,"previous_names":["kennethnym/trycat"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kennethnym%2Ftrycat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kennethnym%2Ftrycat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kennethnym%2Ftrycat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kennethnym%2Ftrycat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kennethnym","download_url":"https://codeload.github.com/kennethnym/trycat/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249016627,"owners_count":21198833,"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-handling","result","result-type","rust-result"],"created_at":"2024-10-01T12:19:35.651Z","updated_at":"2025-04-15T06:10:55.707Z","avatar_url":"https://github.com/kennethnym.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# trycat\n\n`trycat` is a lightweight, type-safe, zero-dependency implementation of the [`Result` type](https://doc.rust-lang.org/std/result/enum.Result.html) in Rust.\nIt provides utilities to replace try-catch error handling with `Result` that provides various methods to\nhandle errors in a more type-safe, explicit manner.\n\n## Usage and Example\n\n`trycat` provides two functions, `trys` and `tryp`, that returns results of throwable synchronous and asynchronous operations as either `Ok` or `Err` respectively.\n\n### `trys`\n\n```ts\nimport * as fs from \"node:fs\"\nimport { type Result, trys } from \"trycat\"\n\nfunction readTextFileSync(path: string): Result\u003cstring, string\u003e {\n  return trys(() =\u003e {\n    return fs.readFileSync(path, \"utf-8\")\n  }).mapErr((err) =\u003e {\n    if (err instance of Error) {\n      return err.message\n    }\n    return \"unknown\"\n  })\n}\n\nconst rows = readTextFileSync(\"./data.csv\")\n  .mapOr([], (content) =\u003e content.split('\\n').map((line) =\u003e line.split(\" \")))\n```\n\nYou can also use the `ok` and `err` functions manually to create an `Ok` value or an `Err` value:\n\n```ts\nimport * as fs from \"node:fs\"\nimport { type Result, ok, err } from \"trycat\"\n\nfunction readTextFileSync(path: string): Result\u003cstring, string\u003e {\n  try {\n    const content = fs.readFileSync(path, \"utf-8\")\n    return ok(content)\n  } catch (err: unknown) {\n    if (err instanceof Error) {\n      return err(err.message)\n    }\n    return err(\"unknown\")\n  }\n}\n\nconst rows = readTextFileSync(\"./data.csv\")\n  .mapOr([], (content) =\u003e content.split('\\n').map((line) =\u003e line.split(\" \")))\n```\n\n### `tryp`\n\n`tryp` is an asynchronous version of `trys`:\n\n```ts\ntype ApiError = \"InternalError\" | \"NetworkError\" | \"ServerError\" | \"UnexpectedResponse\"\n\nconst WeatherSchema = z.object({ ... })\ntype Weather = z.infer\u003ctypeof WeatherSchema\u003e\n\nasync function fetchWeather(): Promise\u003cResult\u003cWeather, ApiError\u003e\u003e {\n  const res = await tryp(fetch(\"/api/weather\"))\n  if (res.isErr()) {\n    return err(\"NetworkError\")\n  }\n  if (res.value.status === 500) {\n    return err(\"ServerError\")\n  }\n\n  const json = await tryp(res.json())\n  if (json.isErr()) {\n    return err(\"UnexpectedResponse\")\n  }\n\n  const weather = trys(() =\u003e WeatherSchema.parse(json)).mapErr((error): ApiError =\u003e {\n    if (error instanceof ZodError) {\n      return \"UnexpectedResponse\"\n    }\n    return \"InternalError\"\n  })\n\n  if (weather.isErr()) {\n    return err(weather.error)\n  }\n\n  return ok(weather.value)\n}\n```\n\n## Matching Rust's Implementation\n\nThe goal of this library is to match Rust's `Result` as close as possible. If there is anything missing, please file an issue.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkennethnym%2Ftrycat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkennethnym%2Ftrycat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkennethnym%2Ftrycat/lists"}