pgerror.go 3.4 KB

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