开发者询问了使用 Go Entgo 和 PostgreSQL 时处理错误的高效方法。Entgo 中提供的 ent.isnotfound、ent.isvalidationerror 等方法只能返回布尔值,而开发者希望获得与 pg 包中 pg.error 类似的更详细的错误信息,包括代码、消息、提示等。
所以我正在使用 go 以及 orm 包 entgo 和 postgres 来开发一个 api。有没有办法以与在 pg 包中解决错误相同或类似的方式处理错误,其中我拥有查询失败的所有所需信息?
我在 ent go 中找到的都是这个方法,但它们只返回 bool
ent.isnotfound(err) ent.isconstrainterror(err) ent.isnotloaded(err) ent.isvalidationerror(err)
如果有类似 pg.error 的东西就太好了:
type Error struct {
Severity string
Code ErrorCode
Message string
Detail string
Hint string
Position string
InternalPosition string
InternalQuery string
Where string
Schema string
Table string
Column string
DataTypeName string
Constraint string
File string
Line string
Routine string
}正确答案
所以我在软件包存储库上问了同样的问题,这是我从存储库维护者那里得到的答案。
Ent 包装或返回底层驱动程序错误。因此,您可以使用类型断言或 error.Is/errors.As 帮助程序来提取这些类型。
查看我们的集成测试中的示例: https://github.com/ent/ent/blob/60e0328/entc/integration/integration_test.go#L1845-L1858
https://github.com/ent/ent/blob/60e0328/entc/integration/integration_test.go#L423
终于介绍完啦!小伙伴们,这篇关于《有没有更高效的方式来处理 Go Entgo与postgreSQL的错误?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
本文内容来源于互联网,如有侵权请联系删除。
