{"id":13412780,"url":"https://github.com/ztrue/tracerr","last_synced_at":"2025-04-10T06:17:42.707Z","repository":{"id":38146507,"uuid":"169459245","full_name":"ztrue/tracerr","owner":"ztrue","description":"Golang errors with stack trace and source fragments.","archived":false,"fork":false,"pushed_at":"2024-05-29T03:24:53.000Z","size":722,"stargazers_count":956,"open_issues_count":3,"forks_count":41,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-07-31T20:51:25.689Z","etag":null,"topics":["debug","error-handling","errors","errors-log","go","golang","source-map","stacktrace"],"latest_commit_sha":null,"homepage":"","language":"Go","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/ztrue.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-02-06T18:57:46.000Z","updated_at":"2024-07-29T09:41:02.000Z","dependencies_parsed_at":"2022-07-14T22:00:37.482Z","dependency_job_id":"ce4950db-fc88-4f08-a006-843537c94815","html_url":"https://github.com/ztrue/tracerr","commit_stats":{"total_commits":15,"total_committers":3,"mean_commits":5.0,"dds":0.1333333333333333,"last_synced_commit":"4635b9d09734c5eb523f97b0fca5b07222584ba1"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ztrue%2Ftracerr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ztrue%2Ftracerr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ztrue%2Ftracerr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ztrue%2Ftracerr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ztrue","download_url":"https://codeload.github.com/ztrue/tracerr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248166859,"owners_count":21058481,"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":["debug","error-handling","errors","errors-log","go","golang","source-map","stacktrace"],"created_at":"2024-07-30T20:01:29.106Z","updated_at":"2025-04-10T06:17:42.680Z","avatar_url":"https://github.com/ztrue.png","language":"Go","readme":"# Golang Errors with Stack Trace and Source Fragments\n\n[![GoDoc](https://godoc.org/github.com/ztrue/tracerr?status.svg)](https://godoc.org/github.com/ztrue/tracerr)\n[![Report](https://goreportcard.com/badge/github.com/ztrue/tracerr)](https://goreportcard.com/report/github.com/ztrue/tracerr)\n[![Coverage Status](https://coveralls.io/repos/github/ztrue/tracerr/badge.svg?branch=master)](https://coveralls.io/github/ztrue/tracerr?branch=master)\n\nTired of uninformative error output? Probably this will be more convenient:\n\n![Output](output.png)\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"io/ioutil\"\n\n\t\"github.com/ztrue/tracerr\"\n)\n\nfunc main() {\n\tif err := read(); err != nil {\n\t\ttracerr.PrintSourceColor(err)\n\t}\n}\n\nfunc read() error {\n\treturn readNonExistent()\n}\n\nfunc readNonExistent() error {\n\t_, err := ioutil.ReadFile(\"/tmp/non_existent_file\")\n\t// Add stack trace to existing error, no matter if it's nil.\n\treturn tracerr.Wrap(err)\n}\n```\n\nFind more executable examples in [examples](examples) dir.\n\n## How to Use\n\n### Import\n\n```go\nimport \"github.com/ztrue/tracerr\"\n```\n\n### Create New Error\n\n```go\nerr := tracerr.New(\"some error\")\n```\n\nOr:\n\n```go\nerr := tracerr.Errorf(\"some error %d\", num)\n```\n\n### Add Stack Trace to Existing Error\n\n\u003e If `err` is `nil` then it still be `nil` with no stack trace added.\n\n```go\nerr = tracerr.Wrap(err)\n```\n\n### Print Error and Stack Trace\n\n\u003e Stack trace will be printed only if `err` is of type `tracerr.Error`, otherwise just error text will be shown.\n\nThis will print error message and stack trace if any:\n\n```go\ntracerr.Print(err)\n```\n\nThis will add source code:\n\n```go\ntracerr.PrintSource(err)\n```\n\nIt's able to set up number of lines of code to display for each frame, which is `6` by default:\n\n```go\ntracerr.PrintSource(err, 9)\n```\n\nOr to set up number of lines before and after traced line:\n\n```go\ntracerr.PrintSource(err, 5, 2)\n```\n\nThe same, but with color, which is much more useful:\n\n```go\ntracerr.PrintSourceColor(err)\n```\n\n```go\ntracerr.PrintSourceColor(err, 9)\n```\n\n```go\ntracerr.PrintSourceColor(err, 5, 2)\n```\n\n### Save Output to Variable\n\nIt's also able to save output to variable instead of printing it, which works the same way:\n\n```go\ntext := tracerr.Sprint(err)\n```\n\n```go\ntext := tracerr.SprintSource(err)\n```\n\n```go\ntext := tracerr.SprintSource(err, 9)\n```\n\n```go\ntext := tracerr.SprintSource(err, 5, 2)\n```\n\n### Get Stack Trace\n\n\u003e Stack trace will be empty if `err` is not an instance of `tracerr.Error`.\n\n```go\nframes := tracerr.StackTrace(err)\n```\n\nOr if `err` is of type `tracerr.Error`:\n\n```go\nframes := err.StackTrace()\n```\n\n### Get Original Error\n\n\u003e Unwrapped error will be `nil` if `err` is `nil` and will be the same error if `err` is not an instance of `tracerr.Error`.\n\n```go\nerr = tracerr.Unwrap(err)\n```\n\nOr if `err` is of type `tracerr.Error`:\n\n```go\nerr = err.Unwrap()\n```\n\n## Performance\n\nStack trace causes a performance overhead, depending on a stack trace depth. This can be insignificant in a number of situations (such as HTTP request handling), however, avoid of adding a stack trace for really hot spots where a high number of errors created frequently, this can be inefficient.\n\n\u003e Benchmarks done on a MacBook Pro 2015 with go 1.11.\n\nBenchmarks for creating a new error with a stack trace of different depth:\n\n```\nBenchmarkNew/5    200000    5646 ns/op    976 B/op   4 allocs/op\nBenchmarkNew/10   200000   11565 ns/op    976 B/op   4 allocs/op\nBenchmarkNew/20    50000   25629 ns/op    976 B/op   4 allocs/op\nBenchmarkNew/40    20000   65833 ns/op   2768 B/op   5 allocs/op\n```\n","funding_links":[],"categories":["Error Handling","Repositories","Go","错误处理","错误处理`go 语言错误处理库`","Relational Databases"],"sub_categories":["Search and Analytic Databases","Advanced Console UIs","检索及分析资料库","SQL 查询语句构建库"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fztrue%2Ftracerr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fztrue%2Ftracerr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fztrue%2Ftracerr/lists"}