有没有更高级的方法来处理Go Entgo postgreSQL错误处理?

huangapple go评论74阅读模式
英文:

Is there a more advance way to handle Go Entgo postgreSQL error handling?

问题

所以我正在使用Go语言和ORM包Entgo以及Postgres开发API。是否有一种处理错误的方式,与pg包中解决查询失败的方式相同或类似,以便我可以获得所有必要的信息?

在ent go中,我找到了以下方法,但它们只返回布尔值:

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
}
英文:

So I'm developing a API using Go together with ORM package Entgo and Postgres. Is there a way to handle the errors same or similar way as they are solved in pg package where I have all needed information why query failed?

All I found in ent go are this methods but they only return bool

ent.IsNotFound(err)
ent.IsConstraintError(err)
ent.IsNotLoaded(err)
ent.IsValidationError(err)

It would be great to have something similar like 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
}

答案1

得分: 2

我在软件包存储库上提出了同样的问题,这是我从存储库维护者那里得到的答案。

Ent包装或返回底层驱动程序的错误。因此,您可以使用类型断言或errors.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

英文:

So I asked this same question on package repo and this is the answer I got from repo Maintainer.

Ent wraps or returns the underlying driver errors. So, you can either use type-assertion or errors.Is/errors.As helpers to extract these types.

See examples in our integration-test:
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

huangapple
  • 本文由 发表于 2022年1月21日 17:01:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/70798737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定