{"id":24083252,"url":"https://github.com/mitranim/try","last_synced_at":"2025-04-30T18:23:00.637Z","repository":{"id":57565203,"uuid":"337032654","full_name":"mitranim/try","owner":"mitranim","description":"[MOVED] Shorter error handling in Go. Supports two styles: explicit \"try\" and exceptions.","archived":false,"fork":false,"pushed_at":"2023-03-26T17:24:10.000Z","size":31,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-26T06:56:57.956Z","etag":null,"topics":["error","error-handling","errors","exceptions","go","golang"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/mitranim/try","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mitranim.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-08T10:06:06.000Z","updated_at":"2023-03-26T17:24:16.000Z","dependencies_parsed_at":"2022-08-23T12:11:31.947Z","dependency_job_id":null,"html_url":"https://github.com/mitranim/try","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Ftry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Ftry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Ftry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Ftry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mitranim","download_url":"https://codeload.github.com/mitranim/try/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251758633,"owners_count":21639072,"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","errors","exceptions","go","golang"],"created_at":"2025-01-09T23:56:11.098Z","updated_at":"2025-04-30T18:23:00.603Z","avatar_url":"https://github.com/mitranim.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"**Moved to https://github.com/mitranim/gg**. This repo is usable but frozen.\n\n## Overview\n\nShorter error handling for Go. Supports two approaches:\n\n* Like the rejected [`try` proposal](https://golang.org/design/32437-try-builtin).\n* Exceptions-based.\n\nFeatures:\n\n* Uses a combination of `defer` and panics to make code _significantly_ shorter, at an acceptable runtime cost.\n* Automatically ensures stacktraces via [\"github.com/pkg/errors\"](https://github.com/pkg/errors).\n* You can choose to keep `error` in signatures and use explicit \"try\".\n* You can choose to drop `error` from signatures and use exceptions.\n\nSee API docs at https://pkg.go.dev/github.com/mitranim/try.\n\n## TOC\n\n* [Why](#why)\n* [Performance](#performance)\n* [Limitations](#limitations)\n* [Naming](#naming)\n* [Changelog](#changelog)\n\n## Why\n\nGo wants you to add meaningful context when handling errors. I sympathize with this idea, and do it often. But there's code where annotating every single failure is not practical and/or bloats the code beyond our ability to _read it back_.\n\n```golang\nfunc someFuncA() error {\n  err := someFuncB()\n  if err != nil {\n    return errors.WithMessage(err, `failed to X`)\n  }\n  err = someFuncC()\n  if err != nil {\n    return errors.WithMessage(err, `failed to X`)\n  }\n  err = someFuncD()\n  if err != nil {\n    return errors.WithMessage(err, `failed to X`)\n  }\n  return nil\n}\n```\n\nUsing the \"try\" style:\n\n```golang\nfunc someFuncA() (err error) {\n  defer try.RecWithMessage(\u0026err, `failed to X`)\n  try.To(someFuncB())\n  try.To(someFuncC())\n  try.To(someFuncD())\n  return\n}\n```\n\nUsing the \"exceptions\" style:\n\n```golang\nfunc someFuncA() {\n  defer try.Detail(`failed to X`)\n  someFuncB()\n  someFuncC()\n  someFuncD()\n}\n```\n\nThe code should speak for itself. This won't be usable for _every_ codebase, see [Limitations](#limitations) below, but can be a nice improvement for some.\n\n## Performance\n\nDefer/panic/recover have no meaningful impact on performance. Generating stacktraces has a very minor performance cost. For most apps and libraries, this makes no difference. For very CPU-heavy code such as low-level image processing, you're free to use defer/panic/recover, but should use errors without stacktraces.\n\n## Limitations\n\nFIXME generics and `gg`\n\nThis package provides a variety of \"try\" functions for common cases, but it can't define something generic like the original proposal did. To make your code compatible, prefer to use pointers for \"inout\" parameters of non-primitive types, and return only `error`:\n\n```golang\nfunc someFunc(input A, out *B) error {\n  *out = someOperation(input)\n  return someErr\n}\n\nvar val B\ntry.To(someFunc(input, \u0026val))\n```\n\n...Or use inout parameters and panics:\n\n```golang\nfunc someFunc(input A, out *B) {\n  *out = someOperation(input)\n}\n\nvar val B\nsomeFunc(input, \u0026val)\n```\n\nIn the current state of Go, functions conforming to this pattern are easier to compose, leading to much shorter code.\n\n## Naming\n\nThe term \"must\" is more conventional in the Go standard library, but this library uses \"try\" because it's more grammatically flexible: \"try string\" works, but \"must string\" would not. The \"try\" proposal used \"try\". Swift error handling is very similar and uses \"try\". (Unlike Swift, we have stacktraces.)\n\n## Changelog\n\n### v0.1.5\n\nBreaking renaming for consistency:\n\n  * `Ignore` → `IgnoreOnly`\n  * `Ignoring` → `IgnoringOnly`\n  * `WithTrans` → `Transing`\n\nAdded:\n\n  * `Ignore`\n  * `Ignoring`\n\n### v0.1.4\n\nBreaking: renamed `Caught` to `CaughtOnly` for consistency, added `Caught`.\n\n### v0.1.3\n\nAdded `DetailOnly` and `DetailOnlyf`.\n\n### v0.1.2\n\nAdded tools to support the \"exceptions\" style. For many apps, it's a better fit than either the Go style or the \"try\" style.\n\n## License\n\nhttps://unlicense.org\n\n## Misc\n\nI'm receptive to suggestions. If this library _almost_ satisfies you but needs changes, open an issue or chat me up. Contacts: https://mitranim.com/#contacts\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitranim%2Ftry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmitranim%2Ftry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitranim%2Ftry/lists"}