| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- // Package pgerror holds SQLSTATE metadata and sentinel errors shared by the
- // parser, executor, and protocol servers.
- package pgerror
- import (
- "errors"
- "fmt"
- )
- // SQLSTATE codes used by PizzaSQL's protocol and HTTP surfaces.
- const (
- CodeFeatureNotSupported = "0A000"
- CodeInvalidText = "22P02"
- CodeInvalidParameter = "22023"
- CodeDatatypeMismatch = "42804"
- CodeUniqueViolation = "23505"
- CodeForeignKeyViolation = "23503"
- CodeCheckViolation = "23514"
- CodeSerializationFailure = "40001"
- CodeLockNotAvailable = "55P03"
- CodeDeadlockDetected = "40P01"
- CodeSyntaxError = "42601"
- CodeUndefinedTable = "42P01"
- CodeInvalidColumnReference = "42P10"
- CodeInternalError = "XX000"
- )
- // ErrUndefinedTable marks a reference to a relation or type name that does not
- // exist. Protocol layers map it to SQLSTATE 42P01 (undefined_table) instead of a
- // generic internal error, which is what a regclass lookup typo must report.
- var ErrUndefinedTable = errors.New("undefined table")
- // UndefinedTable returns an error that wraps ErrUndefinedTable with a
- // client-facing explanation of the name that did not resolve.
- func UndefinedTable(format string, args ...interface{}) error {
- return fmt.Errorf("%w: %s", ErrUndefinedTable, fmt.Sprintf(format, args...))
- }
- // ErrLockNotAvailable marks a NOWAIT locking read whose row is held by another
- // transaction. Protocol layers map it to SQLSTATE 55P03 (lock_not_available).
- var ErrLockNotAvailable = errors.New("lock not available")
- // LockNotAvailable returns a 55P03 error for a contended NOWAIT lock.
- func LockNotAvailable(format string, args ...interface{}) error {
- return fmt.Errorf("%w: %s", ErrLockNotAvailable, fmt.Sprintf(format, args...))
- }
- // ErrDeadlockDetected marks a row-lock wait cycle. Protocol layers map it to
- // SQLSTATE 40P01 (deadlock_detected).
- var ErrDeadlockDetected = errors.New("deadlock detected")
- // DeadlockDetected returns a 40P01 error for a wait cycle.
- func DeadlockDetected(format string, args ...interface{}) error {
- return fmt.Errorf("%w: %s", ErrDeadlockDetected, fmt.Sprintf(format, args...))
- }
- // ErrFeatureNotSupported marks SQL that PizzaSQL intentionally rejects instead
- // of accepting with different semantics. Protocol layers map it to SQLSTATE
- // 0A000 (feature_not_supported) rather than a generic syntax or internal error.
- var ErrFeatureNotSupported = errors.New("feature not supported")
- // Unsupported returns an error that wraps ErrFeatureNotSupported with a
- // client-facing explanation and migration hint.
- func Unsupported(format string, args ...interface{}) error {
- return fmt.Errorf("%w: %s", ErrFeatureNotSupported, fmt.Sprintf(format, args...))
- }
- // ErrInvalidText marks input that is valid SQL but not valid text for the
- // target type, such as a malformed array literal. Protocol layers map it to
- // SQLSTATE 22P02 (invalid_text_representation).
- var ErrInvalidText = errors.New("invalid text representation")
- // InvalidText returns an error that wraps ErrInvalidText with a client-facing
- // explanation of the rejected input.
- func InvalidText(format string, args ...interface{}) error {
- return fmt.Errorf("%w: %s", ErrInvalidText, fmt.Sprintf(format, args...))
- }
- // ErrInvalidParameter marks a well-formed value that is not valid for the
- // operation it is used in, such as jsonb_array_length() on a non-array or a
- // jsonb path element that is NULL. Protocol layers map it to SQLSTATE 22023
- // (invalid_parameter_value).
- var ErrInvalidParameter = errors.New("invalid parameter value")
- // InvalidParameter returns an error that wraps ErrInvalidParameter with a
- // client-facing explanation of the rejected value.
- func InvalidParameter(format string, args ...interface{}) error {
- return fmt.Errorf("%w: %s", ErrInvalidParameter, fmt.Sprintf(format, args...))
- }
- // ErrInvalidColumnReference marks a reference whose shape does not match the
- // relation it names, such as a table-function column alias list with the wrong
- // arity. Protocol layers map it to SQLSTATE 42P10 (invalid_column_reference).
- var ErrInvalidColumnReference = errors.New("invalid column reference")
- // InvalidColumnReference returns an error that wraps
- // ErrInvalidColumnReference with a client-facing explanation.
- func InvalidColumnReference(format string, args ...interface{}) error {
- return fmt.Errorf("%w: %s", ErrInvalidColumnReference, fmt.Sprintf(format, args...))
- }
- // ErrDatatypeMismatch marks a value whose type is wrong for the operation it is
- // used in, such as a non-integer array subscript. Protocol layers map it to
- // SQLSTATE 42804 (datatype_mismatch).
- var ErrDatatypeMismatch = errors.New("datatype mismatch")
- // DatatypeMismatch returns an error that wraps ErrDatatypeMismatch with a
- // client-facing explanation of the incompatible value.
- func DatatypeMismatch(format string, args ...interface{}) error {
- return fmt.Errorf("%w: %s", ErrDatatypeMismatch, fmt.Sprintf(format, args...))
- }
|