mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-30 00:36:14 -06:00
wrap bun.DB in dbConn for dbconn specific error processing etc
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
This commit is contained in:
parent
fc561ef308
commit
0000c2c4a5
18 changed files with 171 additions and 141 deletions
43
internal/db/bundb/errors.go
Normal file
43
internal/db/bundb/errors.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package bundb
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"modernc.org/sqlite"
|
||||
sqlite3 "modernc.org/sqlite/lib"
|
||||
)
|
||||
|
||||
// processPostgresError
|
||||
func processPostgresError(err error) db.Error {
|
||||
// Attempt to cast as postgres
|
||||
pgErr, ok := err.(*pgconn.PgError)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
// Handle supplied error code:
|
||||
// (https://www.postgresql.org/docs/10/errcodes-appendix.html)
|
||||
switch pgErr.Code {
|
||||
case "23505" /* unique_violation */ :
|
||||
return db.ErrAlreadyExists
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// processSQLiteError
|
||||
func processSQLiteError(err error) db.Error {
|
||||
// Attempt to cast as sqlite
|
||||
sqliteErr, ok := err.(*sqlite.Error)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
// Handle supplied error code:
|
||||
switch sqliteErr.Code() {
|
||||
case sqlite3.SQLITE_CONSTRAINT_UNIQUE:
|
||||
return db.ErrAlreadyExists
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue