pgerror.go 2.8 KB

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