blob: 1c0f8a7ffd82d6d418dd972ee295bf22d53f1b3b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package bundb
import (
"errors"
"github.com/jackc/pgconn"
"github.com/mattn/go-sqlite3"
"github.com/superseriousbusiness/gotosocial/internal/db"
)
// processPostgresError processes an error, replacing any postgres specific errors with our own error type
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 processes an error, replacing any sqlite specific errors with our own error type
func processSQLiteError(err error) db.Error {
var sqlError sqlite3.Error
if errors.As(err, &sqlError) {
if sqlError.Code == sqlite3.ErrConstraint && (sqlError.ExtendedCode == sqlite3.ErrConstraintUnique || sqlError.ExtendedCode == sqlite3.ErrConstraintPrimaryKey) {
return db.ErrAlreadyExists
}
}
return err
}
|