2
0

pgerror.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. CodeDatatypeMismatch = "42804"
  13. CodeUniqueViolation = "23505"
  14. CodeForeignKeyViolation = "23503"
  15. CodeCheckViolation = "23514"
  16. CodeSerializationFailure = "40001"
  17. CodeSyntaxError = "42601"
  18. CodeInternalError = "XX000"
  19. )
  20. // ErrFeatureNotSupported marks SQL that PizzaSQL intentionally rejects instead
  21. // of accepting with different semantics. Protocol layers map it to SQLSTATE
  22. // 0A000 (feature_not_supported) rather than a generic syntax or internal error.
  23. var ErrFeatureNotSupported = errors.New("feature not supported")
  24. // Unsupported returns an error that wraps ErrFeatureNotSupported with a
  25. // client-facing explanation and migration hint.
  26. func Unsupported(format string, args ...interface{}) error {
  27. return fmt.Errorf("%w: %s", ErrFeatureNotSupported, fmt.Sprintf(format, args...))
  28. }
  29. // ErrInvalidText marks input that is valid SQL but not valid text for the
  30. // target type, such as a malformed array literal. Protocol layers map it to
  31. // SQLSTATE 22P02 (invalid_text_representation).
  32. var ErrInvalidText = errors.New("invalid text representation")
  33. // InvalidText returns an error that wraps ErrInvalidText with a client-facing
  34. // explanation of the rejected input.
  35. func InvalidText(format string, args ...interface{}) error {
  36. return fmt.Errorf("%w: %s", ErrInvalidText, fmt.Sprintf(format, args...))
  37. }
  38. // ErrDatatypeMismatch marks a value whose type is wrong for the operation it is
  39. // used in, such as a non-integer array subscript. Protocol layers map it to
  40. // SQLSTATE 42804 (datatype_mismatch).
  41. var ErrDatatypeMismatch = errors.New("datatype mismatch")
  42. // DatatypeMismatch returns an error that wraps ErrDatatypeMismatch with a
  43. // client-facing explanation of the incompatible value.
  44. func DatatypeMismatch(format string, args ...interface{}) error {
  45. return fmt.Errorf("%w: %s", ErrDatatypeMismatch, fmt.Sprintf(format, args...))
  46. }