pgerror.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Package pgerror holds SQLSTATE metadata and sentinel errors shared by the
  2. // parser, executor, and protocol servers.
  3. package pgerror
  4. import (
  5. "errors"
  6. "fmt"
  7. )
  8. // SQLSTATE codes used by PizzaSQL's protocol and HTTP surfaces.
  9. const (
  10. CodeFeatureNotSupported = "0A000"
  11. CodeInvalidText = "22P02"
  12. CodeInvalidParameter = "22023"
  13. CodeDatatypeMismatch = "42804"
  14. CodeUniqueViolation = "23505"
  15. CodeForeignKeyViolation = "23503"
  16. CodeCheckViolation = "23514"
  17. CodeSerializationFailure = "40001"
  18. CodeLockNotAvailable = "55P03"
  19. CodeDeadlockDetected = "40P01"
  20. CodeSyntaxError = "42601"
  21. CodeUndefinedTable = "42P01"
  22. CodeInvalidColumnReference = "42P10"
  23. CodeInternalError = "XX000"
  24. )
  25. // ErrUndefinedTable marks a reference to a relation or type name that does not
  26. // exist. Protocol layers map it to SQLSTATE 42P01 (undefined_table) instead of a
  27. // generic internal error, which is what a regclass lookup typo must report.
  28. var ErrUndefinedTable = errors.New("undefined table")
  29. // UndefinedTable returns an error that wraps ErrUndefinedTable with a
  30. // client-facing explanation of the name that did not resolve.
  31. func UndefinedTable(format string, args ...interface{}) error {
  32. return fmt.Errorf("%w: %s", ErrUndefinedTable, fmt.Sprintf(format, args...))
  33. }
  34. // ErrLockNotAvailable marks a NOWAIT locking read whose row is held by another
  35. // transaction. Protocol layers map it to SQLSTATE 55P03 (lock_not_available).
  36. var ErrLockNotAvailable = errors.New("lock not available")
  37. // LockNotAvailable returns a 55P03 error for a contended NOWAIT lock.
  38. func LockNotAvailable(format string, args ...interface{}) error {
  39. return fmt.Errorf("%w: %s", ErrLockNotAvailable, fmt.Sprintf(format, args...))
  40. }
  41. // ErrDeadlockDetected marks a row-lock wait cycle. Protocol layers map it to
  42. // SQLSTATE 40P01 (deadlock_detected).
  43. var ErrDeadlockDetected = errors.New("deadlock detected")
  44. // DeadlockDetected returns a 40P01 error for a wait cycle.
  45. func DeadlockDetected(format string, args ...interface{}) error {
  46. return fmt.Errorf("%w: %s", ErrDeadlockDetected, fmt.Sprintf(format, args...))
  47. }
  48. // ErrFeatureNotSupported marks SQL that PizzaSQL intentionally rejects instead
  49. // of accepting with different semantics. Protocol layers map it to SQLSTATE
  50. // 0A000 (feature_not_supported) rather than a generic syntax or internal error.
  51. var ErrFeatureNotSupported = errors.New("feature not supported")
  52. // Unsupported returns an error that wraps ErrFeatureNotSupported with a
  53. // client-facing explanation and migration hint.
  54. func Unsupported(format string, args ...interface{}) error {
  55. return fmt.Errorf("%w: %s", ErrFeatureNotSupported, fmt.Sprintf(format, args...))
  56. }
  57. // ErrInvalidText marks input that is valid SQL but not valid text for the
  58. // target type, such as a malformed array literal. Protocol layers map it to
  59. // SQLSTATE 22P02 (invalid_text_representation).
  60. var ErrInvalidText = errors.New("invalid text representation")
  61. // InvalidText returns an error that wraps ErrInvalidText with a client-facing
  62. // explanation of the rejected input.
  63. func InvalidText(format string, args ...interface{}) error {
  64. return fmt.Errorf("%w: %s", ErrInvalidText, fmt.Sprintf(format, args...))
  65. }
  66. // ErrInvalidParameter marks a well-formed value that is not valid for the
  67. // operation it is used in, such as jsonb_array_length() on a non-array or a
  68. // jsonb path element that is NULL. Protocol layers map it to SQLSTATE 22023
  69. // (invalid_parameter_value).
  70. var ErrInvalidParameter = errors.New("invalid parameter value")
  71. // InvalidParameter returns an error that wraps ErrInvalidParameter with a
  72. // client-facing explanation of the rejected value.
  73. func InvalidParameter(format string, args ...interface{}) error {
  74. return fmt.Errorf("%w: %s", ErrInvalidParameter, fmt.Sprintf(format, args...))
  75. }
  76. // ErrInvalidColumnReference marks a reference whose shape does not match the
  77. // relation it names, such as a table-function column alias list with the wrong
  78. // arity. Protocol layers map it to SQLSTATE 42P10 (invalid_column_reference).
  79. var ErrInvalidColumnReference = errors.New("invalid column reference")
  80. // InvalidColumnReference returns an error that wraps
  81. // ErrInvalidColumnReference with a client-facing explanation.
  82. func InvalidColumnReference(format string, args ...interface{}) error {
  83. return fmt.Errorf("%w: %s", ErrInvalidColumnReference, fmt.Sprintf(format, args...))
  84. }
  85. // ErrDatatypeMismatch marks a value whose type is wrong for the operation it is
  86. // used in, such as a non-integer array subscript. Protocol layers map it to
  87. // SQLSTATE 42804 (datatype_mismatch).
  88. var ErrDatatypeMismatch = errors.New("datatype mismatch")
  89. // DatatypeMismatch returns an error that wraps ErrDatatypeMismatch with a
  90. // client-facing explanation of the incompatible value.
  91. func DatatypeMismatch(format string, args ...interface{}) error {
  92. return fmt.Errorf("%w: %s", ErrDatatypeMismatch, fmt.Sprintf(format, args...))
  93. }