{"id":20031180,"url":"https://github.com/gohouse/e","last_synced_at":"2025-05-05T04:31:25.396Z","repository":{"id":112508569,"uuid":"215959752","full_name":"gohouse/e","owner":"gohouse","description":"cutome error in golang,golang自定义错误包, 可以记录调用堆栈的具体信息, 包括文件名, 行号, 方法名和错误消息","archived":false,"fork":false,"pushed_at":"2020-09-24T05:48:19.000Z","size":44,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T16:55:07.748Z","etag":null,"topics":["error","error-stack","error-stacktrace","golang","stack"],"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/gohouse.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-10-18T06:53:36.000Z","updated_at":"2023-05-29T10:15:11.000Z","dependencies_parsed_at":"2023-05-15T13:00:18.983Z","dependency_job_id":null,"html_url":"https://github.com/gohouse/e","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gohouse%2Fe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gohouse%2Fe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gohouse%2Fe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gohouse%2Fe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gohouse","download_url":"https://codeload.github.com/gohouse/e/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252439624,"owners_count":21748045,"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-stack","error-stacktrace","golang","stack"],"created_at":"2024-11-13T09:31:26.168Z","updated_at":"2025-05-05T04:31:25.390Z","avatar_url":"https://github.com/gohouse.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n[![GoDoc](https://godoc.org/github.com/gohouse/e?status.svg)](https://godoc.org/github.com/gohouse/e)\n[![Go Report Card](https://goreportcard.com/badge/github.com/gohouse/e)](https://goreportcard.com/report/github.com/gohouse/e)\n[![GitHub release](https://img.shields.io/github/release/gohouse/e.svg)](https://github.com/gohouse/e/releases/latest)\n\n# e\n\ngolang自定义错误包, 可以记录调用位置的具体信息, 包括文件名, 行号, 方法名和错误消息  \ncutome error in golang\n\n## 安装\n\n- go mod \n```shell script\nrequire github.com/gohouse/e master\n```\n\n- go get\n```shell\ngo get github.com/gohouse/e\n```\n\n## 使用\n\n### 生成一个错误\n- 标准生成返回\n```go\nerr := e.New(\"这是错误信息\")\n// 可选第二个参数,设置记录堆栈层数,默认1层\n//err := e.New(\"这是错误信息\", 2)\n```\n\n- 附带接受原始错误的返回\n```go\nerr := e.NewWithError(\"这是错误信息\", error.New(\"这是原生错误信息\"))\n```\n\n- 从原始错误的返回\n```go\nerr := e.NewFromError(error.New(\"这是原生错误信息\"))\n```\n\n### 获取原生标准错误信息\n```go\nerr.Error()\n```\n\n### 获取错误信息包括堆栈\n```go\nerr.ErrorWithStack()\n```\n\n### 获取错误堆栈对象`e.ErrorStack`\n```go\n// 获取堆栈对象\nerrorStack := err.Stack()\n```\n\n### 完整示例\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/gohouse/e\"\n)\n\nfunc main() {\n\tvar err e.Error\n\t// 或者 var err e.Error\n\terr = testError()\n\n\tfmt.Println(\"error msg:\", err.Error())\n\tfmt.Println(\"error stack:\", err.Stack())\n\tfmt.Println(\"error with stack:\", err.ErrorWithStack())\n\n\tfmt.Printf(\"%#v\", err)\n}\n\nfunc testError() e.Error {\n\treturn e.New(\"only show a custom errors demo\", 3)\n}\n```\n输出:  \nerror msg\n```bash\nerror msg: only show a custom errors demo\n```\nerror stack\n```shell script\nerror stack: [{/go/src/github.com/gohouse/e/examples/demo.go 21 main.testError}]\n```\nerror with stack\n```shell script\nerror msg: only show a custom errors demo\nmain.test1112\n    /go/src/github.com/gohouse/e/examples/test.go:20\nmain.test111\n    /go/src/github.com/gohouse/e/examples/test.go:16\nmain.main\n    /go/src/github.com/gohouse/e/examples/test.go:10\n```\n\n\u003e 说明: 为了节约内存占用,默认是记录1层堆栈的信息,如果想要更多堆栈层数,只需要在第二个参数设置对应数量即可,如:  \n```shell script\nfunc testError() e.Error {\n\treturn e.New(\"错误了啦 xxx\", 3)\n}\n```\n`err`会记录3层堆栈信息,对应的 stack 为:\n```shell script\nerror stack: [{/go/src/github.com/gohouse/e/examples/demo.go 21 main.testError} {/go/src/github.com/gohouse/e/examples/demo.go 11 main.main} {/usr/local/go/src/runtime/proc.go 203 runtime.main}]\n```\n\n## 中间件\n我们可以对日志添加中间件,比如做持久化处理,或者打印控制台等\n```go\n// 使用自带的 log 中间件,并设置默认获取错误堆栈层数为3\ne.NewErrorContext().Use(e.Log(\"errors.log\")).Setlayer(3)\ne.New(\"3层错误堆栈测试,并持久化到 errors.log 文件\")\n```\n`e.Log()`中间件代码如下\n```go\n\nfunc Log(fileNames ...string) HandlerFunc {\n\treturn func(ctx *ErrorContext) {\n\t\tvar fileName = \"./error-statck.log\"\n\t\tif len(fileNames) \u003e 0 {\n\t\t\tfileName = fileNames[0]\n\t\t}\n\t\tf, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)\n\t\tif err != nil {\n\t\t\tfmt.Println(err)\n\t\t}\n\t\t//write to the file\n\t\t_, err = fmt.Fprint(f, time.Now().Format(\"[2006-01-02 15:04:05] \"), ctx.ErrorWithStack(), \"--------------------------------------------------\\n\")\n\t\tif err != nil {\n\t\t\tpanic(err.Error())\n\t\t}\n\t\tf.Close()\n\t}\n}\n```\n日志文件记录如下\n```shell script\n[2019-12-07 12:25:30] 3层错误堆栈测试,并持久化到 errors.log 文件\ndemopro/model.IfCheckIn\n    /go/src/demopro/model/check_in.go:21\ndemopro/api.CheckIn\n    /go/src/demopro/api/check_in.go:40\ngithub.com/gin-gonic/gin.(*Context).Next\n    /go/pkg/mod/github.com/gin-gonic/gin@v1.5.0/context.go:147\n--------------------------------------------------\n[2020-02-17 15:42:38] 错误信息实验\nmain.test1112\n    /go/src/github.com/gohouse/e/examples/test.go:20\nmain.test111\n    /go/src/github.com/gohouse/e/examples/test.go:16\nmain.main\n    /go/src/github.com/gohouse/e/examples/test.go:11\n--------------------------------------------------\n```\n## 自定义中间件\n如果我们只是想打印出来,则可以自己定义一个中间件,如下\n```go\nfunc ErrorLog() HandlerFunc {\n\treturn func(ctx *ErrorContext) {\n\t\tlog.Println(ctx.ErrorWithStack())\n\t}\n}\n```\n使用\n```go\ne.NewErrorContext().Use(ErrorLog()).Setlayer(3)\ne.New(\"自定义中间件测试\")\n```\n也可以使用任意多个中间件\n```go\ne.NewErrorContext().Use(xxx(),xxx()).Use(xxx())\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgohouse%2Fe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgohouse%2Fe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgohouse%2Fe/lists"}