当我在 postgres 中遇到错误时,我根本无法检索错误代码号。
在我的程序测试中,我知道我会收到以下错误“pq:重复键值违反唯一约束“associations_pkey””。
查看 postgres 文档,这很可能是 23505 的 pq 错误代码。
我需要在我的 Go 程序中获取该数字,以便我可以检查不同类型的错误并以有用的方式响应最终用户。
但是,我似乎无法掌握 Go 中的错误代码,只有错误消息。我的代码如下:
stmt, _ := DB.Prepare("INSERT INTO table (column_1) VALUES ($1)")
_, err = stmt.Exec("12324354")
if err != nil {
log.Println("Failed to stmt .Exec while trying to insert new association")
log.Println(err.Error())
fmt.Println(err.Code())
} else {
Render.JSON(w, 200, "New row was created succesfully")
}
肥皂起泡泡
相关分类