{"id":26685420,"url":"https://github.com/goapt/errorx","last_synced_at":"2025-03-26T10:16:01.965Z","repository":{"id":57535582,"uuid":"283774956","full_name":"goapt/errorx","owner":"goapt","description":"golang combine errors","archived":false,"fork":false,"pushed_at":"2020-07-31T02:13:42.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-06-20T16:44:36.051Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/goapt.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":"2020-07-30T12:56:37.000Z","updated_at":"2020-07-30T13:18:58.000Z","dependencies_parsed_at":"2022-08-29T00:30:22.505Z","dependency_job_id":null,"html_url":"https://github.com/goapt/errorx","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goapt%2Ferrorx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goapt%2Ferrorx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goapt%2Ferrorx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goapt%2Ferrorx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goapt","download_url":"https://codeload.github.com/goapt/errorx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245632400,"owners_count":20647194,"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":"2025-03-26T10:16:01.396Z","updated_at":"2025-03-26T10:16:01.949Z","avatar_url":"https://github.com/goapt.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# errorx\n\u003ca href=\"https://github.com/goapt/errorx/actions\"\u003e\u003cimg src=\"https://github.com/goapt/errorx/workflows/test/badge.svg\" alt=\"Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://codecov.io/gh/goapt/errorx\"\u003e\u003cimg src=\"https://codecov.io/gh/goapt/errorx/branch/master/graph/badge.svg\" alt=\"codecov\"\u003e\u003c/a\u003e\n\u003ca href=\"https://goreportcard.com/report/github.com/goapt/errorx\"\u003e\u003cimg src=\"https://goreportcard.com/badge/github.com/goapt/errorx\" alt=\"Go Report Card\n\"\u003e\u003c/a\u003e\n\u003ca href=\"https://pkg.go.dev/github.com/goapt/errorx\"\u003e\u003cimg src=\"https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat-square\" alt=\"GoDoc\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opensource.org/licenses/mit-license.php\" rel=\"nofollow\"\u003e\u003cimg src=\"https://badges.frapsoft.com/os/mit/mit.svg?v=103\"\u003e\u003c/a\u003e\n\ngolang combine error package\n\n```shell script\ngo get github.com/goapt/errorx  \n```\n\n## 使用\nerrorx包定义了全局的基本错误如下\n\n* ErrSystem 系统错误，最顶层的错误定义，所有全局定义的错误必须包裹这个错误\n* ErrDatabase 数据库错误\n* ErrRedis Redis错误\n* ErrNetwork 网络错误\n\n其中分别提供了四种错误包裹的快捷方法\n\n* errorx.System(err) 系统错误包裹\n* errorx.Database(err) 数据库错误包裹\n* errorx.Redis(err) Redis错误包裹\n* errorx.Network(err) 网络错误包裹\n\n四种错误包裹都提供额外的msg参数来重置错误信息如下\n\n```go\nerrorx.Database(err, \"订单不存在\")\n```\n\n同样我们保留了`DbPrettyNoMoreRows`和`DbFilterNoMoreRows`两个方法，来辅助快捷的重置无数据错误信息，以及过滤无数据错误\n\n```go\nerrorx.DbPrettyNoMoreRows(err, \"订单不存在\")\nerrorx.DbFilterNoMoreRows(err)\n```\n\n## New一个普通错误，带调用栈\n```go\nerr := errorx.New(\"这是一个错误\")\n```\n\n## 包裹一个普通错误，希望他拥有调用栈\n```go\nerr := errors.New(\"普通的错误\")\nerrorx.Wrap(err)\n```\n\n## 判定错误\n有了包裹错误的方式，我们就可以使用官方的errors.Is来判定错误是否是系统错误，或者数据库错误以及官方包的错误等\n\n```go\nerrors.Is(err,errorx.System)\nerrors.Is(err,errorx.Database)\nerrors.Is(err,sql.ErrNoRows)\n```\n\n## 打印调用堆栈\n有时候我们不仅仅只想获取错误信息，还希望知道错误的调用堆栈，我们可以使用`%+v`来打印如下\n```go\nfmt.Sprintf(\"%+v\",err)\n```\n输出如下\n```\nsql: no rows in result set\ngithub.com/goapt/errorx.TestCombErr_Error.func1\n\t/Users/fifsky/wwwroot/go/github.com/goapt/errorx/error_test.go:57\ngithub.com/goapt/errorx.TestCombErr_Error.func1\n\t/Users/fifsky/wwwroot/go/github.com/goapt/errorx/error_test.go:58\ngithub.com/goapt/errorx.TestCombErr_Error.func1\n\t/Users/fifsky/wwwroot/go/github.com/goapt/errorx/error_test.go:59\ngithub.com/goapt/errorx.TestCombErr_Error.func1\n\t/Users/fifsky/wwwroot/go/github.com/goapt/errorx/error_test.go:60\n```\n\n## 业务层错误\n由于我们业务层统一规范结构如下\n```json\n{\n    \"code\":\"InvalidEmail\",\n    \"msg\": \"无效的邮箱\"\n}\n```\n\n因此我们提供了辅助方法来定义这种结构的错误如下\n\n```go\nvar ErrInvalidEmail = errorx.NewCode(\"InvalidEmail\",\"无效的邮箱\")\n```\n\n第二个参数，支持string,error，因此你可以轻松的包裹一个错误如下\n\n```go\nvar err := errorx.NewCode(\"ConnectFail\",errorx.Database(sql.ErrNoRows))\n//下面都是成立的\nerrors.Is(err,errorx.ErrSystem)\nerrors.Is(err,errorx.ErrDatabase)\nerrors.Is(err,sql.ErrNoRows)\n```\n然后在业务层的response就可以轻松的断言出错误是否为 err.(errorx.CodeError) ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoapt%2Ferrorx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoapt%2Ferrorx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoapt%2Ferrorx/lists"}