Golang postgres error constants?

后端 未结 1 991
一整个雨季
一整个雨季 2021-01-12 15:38

I\'m trying to delete a database using the postgres driver (lib/pq) by doing a:

db.Exec(\"DROP DATABASE dbName;\")

But I\'d like to do a differe

1条回答
  •  一整个雨季
    2021-01-12 15:47

    The lib/pq package may return errors of type *pq.Error, which is a struct. If it does, you may use all its fields to inspect for details of the error.

    This is how it can be done:

    if err, ok := err.(*pq.Error); ok {
        // Here err is of type *pq.Error, you may inspect all its fields, e.g.:
        fmt.Println("pq error:", err.Code.Name())
    }
    

    pq.Error has the following fields:

    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
    }
    

    The meaning and possible values of these fields are Postres specific and the full list can be found here: Error and Notice Message Fields

    0 讨论(0)
提交回复
热议问题