mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-14 11:07:45 +00:00

This PR enhances error handling in the database package by introducing custom error types that wrap dialect-specific errors, providing better abstraction and context for error handling across the application. * Introduces standardized custom error types for common database errors (no rows found, integrity violations, etc.) * Wraps all PostgreSQL-specific errors at the dialect layer to provide consistent error handling # Which Problems Are Solved The database package didn't wrap the errors from dialect specifc packages. # How the Problems Are Solved Custom errors were added which wrap the dialect specifc errors. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
package postgres
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
)
|
|
|
|
func wrapError(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return database.NewNoRowFoundError(err)
|
|
}
|
|
var pgxErr *pgconn.PgError
|
|
if !errors.As(err, &pgxErr) {
|
|
return database.NewUnknownError(err)
|
|
}
|
|
switch pgxErr.Code {
|
|
// 23514: check_violation - A value violates a CHECK constraint.
|
|
case "23514":
|
|
return database.NewCheckError(pgxErr.TableName, pgxErr.ConstraintName, pgxErr)
|
|
// 23505: unique_violation - A value violates a UNIQUE constraint.
|
|
case "23505":
|
|
return database.NewUniqueError(pgxErr.TableName, pgxErr.ConstraintName, pgxErr)
|
|
// 23503: foreign_key_violation - A value violates a foreign key constraint.
|
|
case "23503":
|
|
return database.NewForeignKeyError(pgxErr.TableName, pgxErr.ConstraintName, pgxErr)
|
|
// 23502: not_null_violation - A value violates a NOT NULL constraint.
|
|
case "23502":
|
|
return database.NewNotNullError(pgxErr.TableName, pgxErr.ConstraintName, pgxErr)
|
|
}
|
|
return database.NewUnknownError(err)
|
|
}
|