Files
zitadel/backend/v3/storage/database/errors.go

231 lines
5.8 KiB
Go
Raw Permalink Normal View History

2025-07-17 00:54:21 +02:00
package database
import (
2025-07-22 19:09:56 +02:00
"errors"
2025-07-17 00:54:21 +02:00
"fmt"
)
2025-07-25 20:05:15 +02:00
var ErrNoChanges = errors.New("update must contain a change")
2025-07-22 19:09:56 +02:00
2025-07-17 09:08:33 +02:00
// NoRowFoundError is returned when QueryRow does not find any row.
2025-07-17 00:54:21 +02:00
// It wraps the dialect specific original error to provide more context.
2025-07-17 09:08:33 +02:00
type NoRowFoundError struct {
2025-07-17 00:54:21 +02:00
original error
}
func NewNoRowFoundError(original error) error {
2025-07-17 09:08:33 +02:00
return &NoRowFoundError{
2025-07-17 00:54:21 +02:00
original: original,
}
}
2025-07-17 09:08:33 +02:00
func (e *NoRowFoundError) Error() string {
2025-07-17 00:54:21 +02:00
return "no row found"
}
2025-07-17 09:08:33 +02:00
func (e *NoRowFoundError) Is(target error) bool {
_, ok := target.(*NoRowFoundError)
2025-07-17 00:54:21 +02:00
return ok
}
2025-07-17 09:08:33 +02:00
func (e *NoRowFoundError) Unwrap() error {
2025-07-17 00:54:21 +02:00
return e.original
}
2025-07-17 09:08:33 +02:00
// MultipleRowsFoundError is returned when QueryRow finds multiple rows.
2025-07-17 00:54:21 +02:00
// It wraps the dialect specific original error to provide more context.
2025-07-17 09:08:33 +02:00
type MultipleRowsFoundError struct {
2025-07-17 00:54:21 +02:00
original error
count int
}
func NewMultipleRowsFoundError(original error, count int) error {
2025-07-17 09:08:33 +02:00
return &MultipleRowsFoundError{
2025-07-17 00:54:21 +02:00
original: original,
count: count,
}
}
2025-07-17 09:08:33 +02:00
func (e *MultipleRowsFoundError) Error() string {
2025-07-17 00:54:21 +02:00
return fmt.Sprintf("multiple rows found: %d", e.count)
}
2025-07-17 09:08:33 +02:00
func (e *MultipleRowsFoundError) Is(target error) bool {
_, ok := target.(*MultipleRowsFoundError)
2025-07-17 00:54:21 +02:00
return ok
}
2025-07-17 09:08:33 +02:00
func (e *MultipleRowsFoundError) Unwrap() error {
2025-07-17 00:54:21 +02:00
return e.original
}
type IntegrityType string
const (
IntegrityTypeCheck IntegrityType = "check"
IntegrityTypeUnique IntegrityType = "unique"
IntegrityTypeForeign IntegrityType = "foreign"
IntegrityTypeNotNull IntegrityType = "not null"
)
2025-07-17 09:14:17 +02:00
// IntegrityViolationError represents a generic integrity violation error.
2025-07-17 00:54:21 +02:00
// It wraps the dialect specific original error to provide more context.
2025-07-17 09:14:17 +02:00
type IntegrityViolationError struct {
2025-07-17 00:54:21 +02:00
integrityType IntegrityType
table string
constraint string
original error
}
func NewIntegrityViolationError(typ IntegrityType, table, constraint string, original error) error {
2025-07-17 09:14:17 +02:00
return &IntegrityViolationError{
2025-07-17 00:54:21 +02:00
integrityType: typ,
table: table,
constraint: constraint,
original: original,
}
}
2025-07-17 09:14:17 +02:00
func (e *IntegrityViolationError) Error() string {
2025-07-17 00:54:21 +02:00
return fmt.Sprintf("integrity violation of type %q on %q (constraint: %q): %v", e.integrityType, e.table, e.constraint, e.original)
}
2025-07-17 09:14:17 +02:00
func (e *IntegrityViolationError) Is(target error) bool {
_, ok := target.(*IntegrityViolationError)
2025-07-17 00:54:21 +02:00
return ok
}
2025-07-17 09:08:33 +02:00
// CheckError is returned when a check constraint fails.
2025-07-17 09:14:17 +02:00
// It wraps the [IntegrityViolationError] to provide more context.
2025-07-17 00:54:21 +02:00
// It is used to indicate that a check constraint was violated during an insert or update operation.
2025-07-17 09:08:33 +02:00
type CheckError struct {
2025-07-17 09:14:17 +02:00
IntegrityViolationError
2025-07-17 00:54:21 +02:00
}
func NewCheckError(table, constraint string, original error) error {
2025-07-17 09:08:33 +02:00
return &CheckError{
2025-07-17 09:14:17 +02:00
IntegrityViolationError: IntegrityViolationError{
2025-07-17 00:54:21 +02:00
integrityType: IntegrityTypeCheck,
table: table,
constraint: constraint,
original: original,
},
}
}
2025-07-17 09:08:33 +02:00
func (e *CheckError) Is(target error) bool {
_, ok := target.(*CheckError)
2025-07-17 00:54:21 +02:00
return ok
}
2025-07-17 09:08:33 +02:00
func (e *CheckError) Unwrap() error {
2025-07-17 09:14:17 +02:00
return &e.IntegrityViolationError
2025-07-17 00:54:21 +02:00
}
2025-07-17 09:08:33 +02:00
// UniqueError is returned when a unique constraint fails.
2025-07-17 09:14:17 +02:00
// It wraps the [IntegrityViolationError] to provide more context.
2025-07-17 00:54:21 +02:00
// It is used to indicate that a unique constraint was violated during an insert or update operation.
2025-07-17 09:08:33 +02:00
type UniqueError struct {
2025-07-17 09:14:17 +02:00
IntegrityViolationError
2025-07-17 00:54:21 +02:00
}
func NewUniqueError(table, constraint string, original error) error {
2025-07-17 09:08:33 +02:00
return &UniqueError{
2025-07-17 09:14:17 +02:00
IntegrityViolationError: IntegrityViolationError{
2025-07-17 00:54:21 +02:00
integrityType: IntegrityTypeUnique,
table: table,
constraint: constraint,
original: original,
},
}
}
2025-07-17 09:08:33 +02:00
func (e *UniqueError) Is(target error) bool {
_, ok := target.(*UniqueError)
2025-07-17 00:54:21 +02:00
return ok
}
2025-07-17 09:08:33 +02:00
func (e *UniqueError) Unwrap() error {
2025-07-17 09:14:17 +02:00
return &e.IntegrityViolationError
2025-07-17 00:54:21 +02:00
}
2025-07-17 09:08:33 +02:00
// ForeignKeyError is returned when a foreign key constraint fails.
2025-07-17 09:14:17 +02:00
// It wraps the [IntegrityViolationError] to provide more context.
2025-07-17 00:54:21 +02:00
// It is used to indicate that a foreign key constraint was violated during an insert or update operation
2025-07-17 09:08:33 +02:00
type ForeignKeyError struct {
2025-07-17 09:14:17 +02:00
IntegrityViolationError
2025-07-17 00:54:21 +02:00
}
func NewForeignKeyError(table, constraint string, original error) error {
2025-07-17 09:08:33 +02:00
return &ForeignKeyError{
2025-07-17 09:14:17 +02:00
IntegrityViolationError: IntegrityViolationError{
2025-07-17 00:54:21 +02:00
integrityType: IntegrityTypeForeign,
table: table,
constraint: constraint,
original: original,
},
}
}
2025-07-17 09:08:33 +02:00
func (e *ForeignKeyError) Is(target error) bool {
_, ok := target.(*ForeignKeyError)
2025-07-17 00:54:21 +02:00
return ok
}
2025-07-17 09:08:33 +02:00
func (e *ForeignKeyError) Unwrap() error {
2025-07-17 09:14:17 +02:00
return &e.IntegrityViolationError
2025-07-17 00:54:21 +02:00
}
2025-07-17 09:08:33 +02:00
// NotNullError is returned when a not null constraint fails.
2025-07-17 09:14:17 +02:00
// It wraps the [IntegrityViolationError] to provide more context.
2025-07-17 00:54:21 +02:00
// It is used to indicate that a not null constraint was violated during an insert or update operation.
2025-07-17 09:08:33 +02:00
type NotNullError struct {
2025-07-17 09:14:17 +02:00
IntegrityViolationError
2025-07-17 00:54:21 +02:00
}
func NewNotNullError(table, constraint string, original error) error {
2025-07-17 09:08:33 +02:00
return &NotNullError{
2025-07-17 09:14:17 +02:00
IntegrityViolationError: IntegrityViolationError{
2025-07-17 00:54:21 +02:00
integrityType: IntegrityTypeNotNull,
table: table,
constraint: constraint,
original: original,
},
}
}
2025-07-17 09:08:33 +02:00
func (e *NotNullError) Is(target error) bool {
_, ok := target.(*NotNullError)
2025-07-17 00:54:21 +02:00
return ok
}
2025-07-17 09:08:33 +02:00
func (e *NotNullError) Unwrap() error {
2025-07-17 09:14:17 +02:00
return &e.IntegrityViolationError
2025-07-17 00:54:21 +02:00
}
2025-07-17 09:08:33 +02:00
// UnknownError is returned when an unknown error occurs.
2025-07-17 00:54:21 +02:00
// It wraps the dialect specific original error to provide more context.
// It is used to indicate that an error occurred that does not fit into any of the other categories.
2025-07-17 09:08:33 +02:00
type UnknownError struct {
2025-07-17 00:54:21 +02:00
original error
}
func NewUnknownError(original error) error {
2025-07-17 09:08:33 +02:00
return &UnknownError{
2025-07-17 00:54:21 +02:00
original: original,
}
}
2025-07-17 09:08:33 +02:00
func (e *UnknownError) Error() string {
2025-07-17 01:04:52 +02:00
return fmt.Sprintf("unknown database error: %v", e.original)
2025-07-17 00:54:21 +02:00
}
2025-07-17 09:08:33 +02:00
func (e *UnknownError) Is(target error) bool {
_, ok := target.(*UnknownError)
2025-07-17 00:54:21 +02:00
return ok
}
2025-07-17 09:08:33 +02:00
func (e *UnknownError) Unwrap() error {
2025-07-17 00:54:21 +02:00
return e.original
}