{"id":18775992,"url":"https://github.com/way29/errors","last_synced_at":"2025-04-13T09:31:39.811Z","repository":{"id":57633360,"uuid":"414036134","full_name":"WAY29/errors","owner":"WAY29","description":"Just another error handling primitives for golang","archived":false,"fork":false,"pushed_at":"2021-10-09T16:03:00.000Z","size":30,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-06-21T20:05:24.631Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WAY29.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}},"created_at":"2021-10-06T01:51:45.000Z","updated_at":"2022-02-19T10:04:45.000Z","dependencies_parsed_at":"2022-08-31T16:32:00.708Z","dependency_job_id":null,"html_url":"https://github.com/WAY29/errors","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WAY29%2Ferrors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WAY29%2Ferrors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WAY29%2Ferrors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WAY29%2Ferrors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WAY29","download_url":"https://codeload.github.com/WAY29/errors/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223580153,"owners_count":17168578,"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":[],"created_at":"2024-11-07T19:44:27.156Z","updated_at":"2024-11-07T19:44:27.780Z","avatar_url":"https://github.com/WAY29.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# errors\n\nJust another error handling primitives for golang\n\n## Install\n```\ngo install github.com/WAY29/errors@latest\n```\n## Usage\n### New error and print error context\nThe **errors.New()/errors.Newf()** function returns a new error that has a context, and you can use \"%+v\" as format descriptor to print error context. For example\n```go\nerr := errors.New(\"an_error\")\nfmt.Printf(\"%+v\", err) // will print error message and context information\n```\n\n### Print error message\n```go\nerr := errors.New(\"an_error\")\nfmt.Printf(\"%+v\", err) // will print error message and context information\nfmt.Printf(\"%#v\", err) // will only print error context information\nfmt.Printf(\"%v\", err) // will only print error message\nfmt.Printf(\"%s\", err) // will only print error message\n```\n### Adding context to an error\nThe **errors.Wrap()/errors.Wrapf()** function returns a new error that adds context to the original error. For example\n```go\n_, err := ioutil.ReadAll(r)\nif err != nil {\n        return errors.Wrap(err, \"read failed\")\n}\n```\n### Adding error type to an error\nThe **errors.SetType()** function returns a new error that adds error type to wrapped error. For example\n```go\ntype ErrorType uint16\n\nconst (\n\tRequestError ErrorType = iota\n\tResponseError\n)\n\n\nfunc test() {\n\terr := errors.New(\"new error\")\n\terr, _ = errors.SetType(err, RequestError)\n\t// or errors.SetTypeWithoutBool\n\t// err = errors.SetTypeWithoutBool(err, RequestError)\n\n\tswitch errType := errors.GetType(err); errType {\n\tcase RequestError:\n\t\tfmt.Printf(\"Request error: %+v\\n\", err)\n\tcase ResponseError:\n\t\tfmt.Printf(\"Response error: %+v\\n\", err)\n\tdefault:\n\t\tfmt.Printf(\"Unknown error: %#v\\n\", err)\n\t}\n}\n```\n\n### Adding more information to an error\nThe **errors.Wrap()/errors.Wrapf()** function can also be used to add additional information to wrapped errors. For example\n```go\nerr := errors.New(\"an_error\")\nerr = errors.Wrap(err, \"more information\")\nfmt.Printf(\"%+v\", err) // will print error message and context information\n```\n\n### Retrieving the cause of an error\nUsing **errors.Wrap()** constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by errors.Cause.\n```go\ntype causer interface {\n        Cause() error\n}\n```\nerrors.Cause will recursively retrieve the topmost error which does not implement causer, which is assumed to be the original cause. For example:\n```go\nswitch err := errors.Cause(err).(type) {\ncase *MyError:\n        // handle specifically\ndefault:\n        // unknown error\n}\n```\n\n### How I use this library\nFor example\n```go\npackage errors\n\nimport (\n\t\"fmt\"\n\t\"github.com/WAY29/errors\"\n)\n\ntype ErrorType uint16\n\nconst (\n\tRequestError ErrorType = iota\n\tResponseError\n)\n\nfunc main() {\n\t// errors.SetCurrentAbsPath()\n\n\terr := errors.New(\"new error\")\n\terr, _ = errors.SetType(err, RequestError)\n\terr = errors.Wrapf(err, \"wrapped\")\n\n\tswitch errType := errors.GetType(err); errType {\n\tcase RequestError:\n\t\tfmt.Printf(\"Request error: %+v\\n\", err)\n\tcase ResponseError:\n\t\tfmt.Printf(\"Response error: %+v\\n\", err)\n\tdefault:\n\t\tfmt.Printf(\"Unknown error: %#v\\n\", err)\n\t}\n}\n\n```\n\n## Benchmark\n```\nBenchmarkError1000\nBenchmarkError1000-16\n 2483836\t       461.6 ns/op\t      48 B/op\t       2 allocs/op\nBenchmarkError10000\nBenchmarkError10000-16\n 2560116\t       459.6 ns/op\t      48 B/op\t       2 allocs/op\nBenchmarkWrappedError1000\nBenchmarkWrappedError1000-16\n  933684\t      1283 ns/op\t     360 B/op\t      11 allocs/op\nBenchmarkWrappedError10000\nBenchmarkWrappedError10000-16\n  958335\t      1263 ns/op\t     360 B/op\t      11 allocs/op\n```\n\n## Notice\n- If you run golang files by `go run`, please run `errors.SetCurrentAbsPath()` first, or stack message about path will be absolute path.\n- If you want to skip some frame about stack, please run `errors.SetSkipFrameNum(skipNum)`, this is usually used for your secondary encapsulation of the library.\n\n## Reference\n- [pkg/errors](https://github.com/pkg/errors)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fway29%2Ferrors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fway29%2Ferrors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fway29%2Ferrors/lists"}