如何在Go语言中解释PostgreSQL的错误信息?

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

How do I interpret PostgreSQL error messages from within Go?

问题

如何在Go语言中解析SQL约束错误,特别是由于UNIQUEFOREIGN KEYNOT NULLON DELETE RESTRICT等约束引起的错误?

例如,在数据库中定义了一个UNIQUE字段的情况下,插入一个可能具有相同电子邮件的新用户。在Go中解析约束错误,并将错误返回给浏览器客户端。

英文:

How can I parse SQL constraint errors, in particular resulting from constraints such as UNIQUE, FOREIGN KEY, NOT NULL, ON DELETE RESTRICT within Go?

e.g. Insert a new user that may have the same email which is defined as a UNIQUE field in the database. Parse constraint error in Go, return error to browser client.

答案1

得分: 6

有关如何解释来自github.com/lib/pq的错误的信息,请参阅http://godoc.org/github.com/lib/pq#Error。

这是我要做的:

// ShowError发送适当的错误消息。
func ShowError(w http.ResponseWriter, r *http.Request, err error) {
switch e := err.(type) {
case *pq.Error:
switch e.Code {
case "23502":
// not-null constraint violation
http.Error(w, fmt.Sprint("Some required data was left out:\n\n", e.Message), http.StatusForbidden)
return

  1. case "23503":
  2. // foreign key violation
  3. switch r.Method {
  4. case "DELETE":
  5. http.Error(w, fmt.Sprint("This record can’t be deleted because another record refers to it:\n\n", e.Detail), http.StatusForbidden)
  6. return
  7. }
  8. case "23505":
  9. // unique constraint violation
  10. http.Error(w, fmt.Sprint("This record contains duplicated data that conflicts with what is already in the database:\n\n", e.Detail), http.StatusForbidden)
  11. return
  12. case "23514":
  13. // check constraint violation
  14. http.Error(w, fmt.Sprint("This record contains inconsistent or out-of-range data:\n\n", e.Message), http.StatusForbidden)
  15. return
  16. default:
  17. msg := e.Message
  18. if d := e.Detail; d != "" {
  19. msg += "\n\n" + d
  20. }
  21. if h := e.Hint; h != "" {
  22. msg += "\n\n" + h
  23. }
  24. http.Error(w, msg, http.StatusInternalServerError)
  25. return
  26. }
  27. case *strconv.NumError:
  28. http.Error(w, fmt.Sprintf(`"%s" is not a valid number.`, e.Num), http.StatusBadRequest)
  29. return
  30. default:
  31. switch err {
  32. case sql.ErrNoRows:
  33. http.NotFound(w, r)
  34. return
  35. }
  36. }
  37. http.Error(w, err.Error(), http.StatusInternalServerError)

}

英文:

For information about how to interpret errors from github.com/lib/pq, see http://godoc.org/github.com/lib/pq#Error.

Here is what I do:

  1. // ShowError sends an appropriate error message.
  2. func ShowError(w http.ResponseWriter, r *http.Request, err error) {
  3. switch e := err.(type) {
  4. case *pq.Error:
  5. switch e.Code {
  6. case "23502":
  7. // not-null constraint violation
  8. http.Error(w, fmt.Sprint("Some required data was left out:\n\n", e.Message), http.StatusForbidden)
  9. return
  10. case "23503":
  11. // foreign key violation
  12. switch r.Method {
  13. case "DELETE":
  14. http.Error(w, fmt.Sprint("This record cant be deleted because another record refers to it:\n\n", e.Detail), http.StatusForbidden)
  15. return
  16. }
  17. case "23505":
  18. // unique constraint violation
  19. http.Error(w, fmt.Sprint("This record contains duplicated data that conflicts with what is already in the database:\n\n", e.Detail), http.StatusForbidden)
  20. return
  21. case "23514":
  22. // check constraint violation
  23. http.Error(w, fmt.Sprint("This record contains inconsistent or out-of-range data:\n\n", e.Message), http.StatusForbidden)
  24. return
  25. default:
  26. msg := e.Message
  27. if d := e.Detail; d != "" {
  28. msg += "\n\n" + d
  29. }
  30. if h := e.Hint; h != "" {
  31. msg += "\n\n" + h
  32. }
  33. http.Error(w, msg, http.StatusInternalServerError)
  34. return
  35. }
  36. case *strconv.NumError:
  37. http.Error(w, fmt.Sprintf(`"%s" is not a valid number.`, e.Num), http.StatusBadRequest)
  38. return
  39. default:
  40. switch err {
  41. case sql.ErrNoRows:
  42. http.NotFound(w, r)
  43. return
  44. }
  45. }
  46. http.Error(w, err.Error(), http.StatusInternalServerError)
  47. }

huangapple
  • 本文由 发表于 2015年4月30日 02:24:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/29951615.html
匿名

发表评论

匿名网友

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

确定