errors.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package sqlerr
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. var Exists = errors.New("already exists")
  7. var NotFound = errors.New("does not exist")
  8. var NotUnique = errors.New("is not unique")
  9. type Error struct {
  10. Err error
  11. Code string
  12. Message string
  13. Location int
  14. Line int
  15. Column int
  16. // Hint string
  17. }
  18. func (e *Error) Unwrap() error {
  19. return e.Err
  20. }
  21. func (e *Error) Error() string {
  22. if e.Err != nil {
  23. return fmt.Sprintf("%s %s", e.Message, e.Err.Error())
  24. } else {
  25. return e.Message
  26. }
  27. }
  28. func ColumnExists(rel, col string) *Error {
  29. return &Error{
  30. Err: Exists,
  31. Code: "42701",
  32. Message: fmt.Sprintf("column \"%s\" of relation \"%s\"", col, rel),
  33. }
  34. }
  35. func ColumnNotFound(rel, col string) *Error {
  36. return &Error{
  37. Err: NotFound,
  38. Code: "42703",
  39. Message: fmt.Sprintf("column \"%s\" of relation \"%s\"", col, rel),
  40. }
  41. }
  42. func RelationExists(rel string) *Error {
  43. return &Error{
  44. Err: Exists,
  45. Code: "42P07",
  46. Message: fmt.Sprintf("relation \"%s\"", rel),
  47. }
  48. }
  49. func RelationNotFound(rel string) *Error {
  50. return &Error{
  51. Err: NotFound,
  52. Code: "42P01",
  53. Message: fmt.Sprintf("relation \"%s\"", rel),
  54. }
  55. }
  56. func SchemaExists(name string) *Error {
  57. return &Error{
  58. Err: Exists,
  59. Code: "42P06",
  60. Message: fmt.Sprintf("schema \"%s\"", name),
  61. }
  62. }
  63. func SchemaNotFound(sch string) *Error {
  64. return &Error{
  65. Err: NotFound,
  66. Code: "3F000",
  67. Message: fmt.Sprintf("schema \"%s\"", sch),
  68. }
  69. }
  70. func TypeExists(typ string) *Error {
  71. return &Error{
  72. Err: Exists,
  73. Code: "42710",
  74. Message: fmt.Sprintf("type \"%s\"", typ),
  75. }
  76. }
  77. func TypeNotFound(typ string) *Error {
  78. return &Error{
  79. Err: NotFound,
  80. Code: "42704",
  81. Message: fmt.Sprintf("type \"%s\"", typ),
  82. }
  83. }
  84. func FunctionNotUnique(fn string) *Error {
  85. return &Error{
  86. Err: NotUnique,
  87. Message: fmt.Sprintf("function name \"%s\"", fn),
  88. }
  89. }