chore(database): enhance error handling (#10279)

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>
This commit is contained in:
Silvan
2025-07-17 16:20:02 +02:00
committed by GitHub
parent 75a04e83ae
commit 7708fdbbf4
12 changed files with 351 additions and 95 deletions

View File

@@ -10,10 +10,29 @@ import (
var (
_ database.Rows = (*Rows)(nil)
_ database.CollectableRows = (*Rows)(nil)
_ database.Row = (*Row)(nil)
)
type Row struct{ pgx.Row }
// Scan implements [database.Row].
// Subtle: this method shadows the method ([pgx.Row]).Scan of Row.Row.
func (r *Row) Scan(dest ...any) error {
return wrapError(r.Row.Scan(dest...))
}
type Rows struct{ pgx.Rows }
// Err implements [database.Rows].
// Subtle: this method shadows the method ([pgx.Rows]).Err of Rows.Rows.
func (r *Rows) Err() error {
return wrapError(r.Rows.Err())
}
func (r *Rows) Scan(dest ...any) error {
return wrapError(r.Rows.Scan(dest...))
}
// Collect implements [database.CollectableRows].
// See [this page](https://github.com/georgysavva/scany/blob/master/dbscan/doc.go#L8) for additional details.
func (r *Rows) Collect(dest any) (err error) {
@@ -23,7 +42,7 @@ func (r *Rows) Collect(dest any) (err error) {
err = closeErr
}
}()
return pgxscan.ScanAll(dest, r.Rows)
return wrapError(pgxscan.ScanAll(dest, r.Rows))
}
// CollectFirst implements [database.CollectableRows].
@@ -35,7 +54,7 @@ func (r *Rows) CollectFirst(dest any) (err error) {
err = closeErr
}
}()
return pgxscan.ScanRow(dest, r.Rows)
return wrapError(pgxscan.ScanRow(dest, r.Rows))
}
// CollectExactlyOneRow implements [database.CollectableRows].
@@ -47,7 +66,7 @@ func (r *Rows) CollectExactlyOneRow(dest any) (err error) {
err = closeErr
}
}()
return pgxscan.ScanOne(dest, r.Rows)
return wrapError(pgxscan.ScanOne(dest, r.Rows))
}
// Close implements [database.Rows].