pgerror.go 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. CodeSyntaxError = "42601"
  19. CodeUndefinedTable = "42P01"
  20. CodeInvalidColumnReference = "42P10"
  21. CodeInternalError = "XX000"
  22. )
  23. // ErrUndefinedTable marks a reference to a relation or type name that does not
  24. // exist. Protocol layers map it to SQLSTATE 42P01 (undefined_table) instead of a
  25. // generic internal error, which is what a regclass lookup typo must report.
  26. var ErrUndefinedTable = errors.New("undefined table")
  27. // UndefinedTable returns an error that wraps ErrUndefinedTable with a
  28. // client-facing explanation of the name that did not resolve.
  29. func UndefinedTable(format string, args ...interface{}) error {
  30. return fmt.Errorf("%w: %s", ErrUndefinedTable, fmt.Sprintf(format, args...))
  31. }
  32. // ErrFeatureNotSupported marks SQL that PizzaSQL intentionally rejects instead
  33. // of accepting with different semantics. Protocol layers map it to SQLSTATE
  34. // 0A000 (feature_not_supported) rather than a generic syntax or internal error.
  35. var ErrFeatureNotSupported = errors.New("feature not supported")
  36. // Unsupported returns an error that wraps ErrFeatureNotSupported with a
  37. // client-facing explanation and migration hint.
  38. func Unsupported(format string, args ...interface{}) error {
  39. return fmt.Errorf("%w: %s", ErrFeatureNotSupported, fmt.Sprintf(format, args...))
  40. }
  41. // ErrInvalidText marks input that is valid SQL but not valid text for the
  42. // target type, such as a malformed array literal. Protocol layers map it to
  43. // SQLSTATE 22P02 (invalid_text_representation).
  44. var ErrInvalidText = errors.New("invalid text representation")
  45. // InvalidText returns an error that wraps ErrInvalidText with a client-facing
  46. // explanation of the rejected input.
  47. func InvalidText(format string, args ...interface{}) error {
  48. return fmt.Errorf("%w: %s", ErrInvalidText, fmt.Sprintf(format, args...))
  49. }
  50. // ErrInvalidParameter marks a well-formed value that is not valid for the
  51. // operation it is used in, such as jsonb_array_length() on a non-array or a
  52. // jsonb path element that is NULL. Protocol layers map it to SQLSTATE 22023
  53. // (invalid_parameter_value).
  54. var ErrInvalidParameter = errors.New("invalid parameter value")
  55. // InvalidParameter returns an error that wraps ErrInvalidParameter with a
  56. // client-facing explanation of the rejected value.
  57. func InvalidParameter(format string, args ...interface{}) error {
  58. return fmt.Errorf("%w: %s", ErrInvalidParameter, fmt.Sprintf(format, args...))
  59. }
  60. // ErrInvalidColumnReference marks a reference whose shape does not match the
  61. // relation it names, such as a table-function column alias list with the wrong
  62. // arity. Protocol layers map it to SQLSTATE 42P10 (invalid_column_reference).
  63. var ErrInvalidColumnReference = errors.New("invalid column reference")
  64. // InvalidColumnReference returns an error that wraps
  65. // ErrInvalidColumnReference with a client-facing explanation.
  66. func InvalidColumnReference(format string, args ...interface{}) error {
  67. return fmt.Errorf("%w: %s", ErrInvalidColumnReference, fmt.Sprintf(format, args...))
  68. }
  69. // ErrDatatypeMismatch marks a value whose type is wrong for the operation it is
  70. // used in, such as a non-integer array subscript. Protocol layers map it to
  71. // SQLSTATE 42804 (datatype_mismatch).
  72. var ErrDatatypeMismatch = errors.New("datatype mismatch")
  73. // DatatypeMismatch returns an error that wraps ErrDatatypeMismatch with a
  74. // client-facing explanation of the incompatible value.
  75. func DatatypeMismatch(format string, args ...interface{}) error {
  76. return fmt.Errorf("%w: %s", ErrDatatypeMismatch, fmt.Sprintf(format, args...))
  77. }