{"id":13600927,"url":"https://github.com/fatih/errwrap","last_synced_at":"2025-04-05T00:10:23.345Z","repository":{"id":42207642,"uuid":"225055874","full_name":"fatih/errwrap","owner":"fatih","description":"Go tool to wrap and fix errors with the new %w verb directive","archived":false,"fork":false,"pushed_at":"2024-06-20T07:15:50.000Z","size":37,"stargazers_count":373,"open_issues_count":1,"forks_count":16,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-28T23:08:41.599Z","etag":null,"topics":["analyzer","errwrap","go","golang","tools"],"latest_commit_sha":null,"homepage":"https://arslan.io","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fatih.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":"2019-11-30T18:48:29.000Z","updated_at":"2024-12-26T09:10:32.000Z","dependencies_parsed_at":"2024-01-07T01:19:17.296Z","dependency_job_id":"d771163b-6cb2-484f-9cc7-86c4d3aeef27","html_url":"https://github.com/fatih/errwrap","commit_stats":{"total_commits":25,"total_committers":8,"mean_commits":3.125,"dds":0.36,"last_synced_commit":"6476f5a6c03eee83aa43324d715073efecdd230e"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatih%2Ferrwrap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatih%2Ferrwrap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatih%2Ferrwrap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatih%2Ferrwrap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fatih","download_url":"https://codeload.github.com/fatih/errwrap/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266565,"owners_count":20910836,"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":["analyzer","errwrap","go","golang","tools"],"created_at":"2024-08-01T18:00:51.000Z","updated_at":"2025-04-05T00:10:23.330Z","avatar_url":"https://github.com/fatih.png","language":"Go","readme":"# errwrap [![](https://github.com/fatih/errwrap/workflows/build/badge.svg)](https://github.com/fatih/errwrap/actions)\n\nWrap and fix Go errors with the new **`%w`** verb directive. This tool analyzes\n`fmt.Errorf()` calls and reports calls that contain a verb directive that is\ndifferent than the new `%w` verb directive [introduced in Go v1.13](https://golang.org/doc/go1.13#error_wrapping). It's also capable of rewriting calls to use the new `%w` wrap verb directive.\n\n![errwrap](https://user-images.githubusercontent.com/438920/69905498-26b34c80-1369-11ea-888d-608f32678971.gif)\n\n## Install\n\n```bash\n# minimum v1.16 is required\ngo install github.com/fatih/errwrap@latest\n```\n\nor download one of the [pre-compiled binaries from the releases page](https://github.com/fatih/errwrap/releases/latest) and copy to the desired location.\n\n## Usage\n\nBy default, `errwrap` prints the output of the analyzer to stdout. You can pass\na file, directory or a Go package:\n\n```sh\n$ errwrap foo.go # pass a file\n$ errwrap ./...  # recursively analyze all files\n$ errwrap github.com/fatih/gomodifytags # or pass a package\n```\n\nWhen called it displays the error with the line and column:\n\n```\ngomodifytags@v1.0.1/main.go:200:16: call could wrap the error with error-wrapping directive %w\ngomodifytags@v1.0.1/main.go:641:17: call could wrap the error with error-wrapping directive %w\ngomodifytags@v1.0.1/main.go:749:15: call could wrap the error with error-wrapping directive %w\n```\n\n`errwrap` is also able to rewrite your source code to replace any verb\ndirective used for an `error` type with the `%w` verb directive. Assume we have\nthe following source code:\n\n```\n$ cat demo.go\npackage main\n\nimport (\n        \"errors\"\n        \"fmt\"\n)\n\nfunc main() {\n        _ = foo()\n}\n\nfunc foo() error {\n        err := errors.New(\"bar!\")\n        return fmt.Errorf(\"foo failed: %s: %s bar ...\", \"foo\", err)\n}\n```\n\nCalling `errwrap` with the `-fix` flag will rewrite the source code:\n\n```\n$ errwrap -fix main.go\nmain.go:14:9: call could wrap the error with error-wrapping directive %w\n```\n\n```diff\ndiff --git a/main.go b/main.go\nindex 41d1c42..6cb42b8 100644\n--- a/main.go\n+++ b/main.go\n@@ -11,5 +11,5 @@ func main() {\n\n func foo() error {\n        err := errors.New(\"bar!\")\n-       return fmt.Errorf(\"failed for %s with error: %s\", \"foo\", err)\n+       return fmt.Errorf(\"failed for %s with error: %w\", \"foo\", err)\n }\n```\n\n## Whether to Wrap or not?\n\nWrapping an error is not always the best approach. Wrapping exposes the\nunderlying error and makes it part of your public API. This means clients who\nrely on them could see breaking changes if you change your underlying\nimplementation or don't wrap anymore.\n\nThe blog post **[Working with Errors in Go\n1.13](https://blog.golang.org/go1.13-errors)** contains a section called\n`Whether to Wrap` that explains this in more detail\n\n\n## Credits\n\nThis tool is built on top of the excellent `go/analysis` package that makes it\neasy to write custom analyzers in Go. If you're interested in writing a tool,\ncheck out my **[Using go/analysis to write a custom\nlinter](https://arslan.io/2019/06/13/using-go-analysis-to-write-a-custom-linter/)**\nblog post.\n\nAlso part of the code that parses the verb directives is from the\n`go/analysis/passes/printf` analyzer. It's a simplified version and might\ncontain discrepancies.\n","funding_links":[],"categories":["开源类库","Go","Open source library","Repositories","Programming Languages"],"sub_categories":["错误处理","Error Handling"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffatih%2Ferrwrap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffatih%2Ferrwrap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffatih%2Ferrwrap/lists"}