-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
error.go
64 lines (53 loc) · 1.63 KB
/
error.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package sqlcon
import (
"database/sql"
"net/http"
"strings"
"github.com/go-sql-driver/mysql"
"github.com/jackc/pgconn"
"github.com/pkg/errors"
"github.com/ory/herodot"
"github.com/ory/x/errorsx"
)
var (
// ErrUniqueViolation is returned when^a SQL INSERT / UPDATE command returns a conflict.
ErrUniqueViolation = &herodot.DefaultError{
CodeField: http.StatusConflict,
StatusField: http.StatusText(http.StatusConflict),
ErrorField: "Unable to insert or update resource because a resource with that value exists already",
}
// ErrNoRows is returned when a SQL SELECT statement returns no rows.
ErrNoRows = &herodot.DefaultError{
CodeField: http.StatusNotFound,
StatusField: http.StatusText(http.StatusNotFound),
ErrorField: "Unable to locate the resource",
}
)
// HandleError returns the right sqlcon.Err* depending on the input error.
func HandleError(err error) error {
if err == nil {
return nil
}
if errorsx.Cause(err) == sql.ErrNoRows {
return errors.WithStack(ErrNoRows)
}
if err, ok := errorsx.Cause(err).(*pgconn.PgError); ok {
switch err.Code {
case "23505": // "unique_violation"
return errors.Wrap(ErrUniqueViolation, err.Error())
}
return errors.WithStack(err)
}
if err, ok := errorsx.Cause(err).(*mysql.MySQLError); ok {
switch err.Number {
case 1062:
return errors.Wrap(ErrUniqueViolation, err.Error())
}
return errors.WithStack(err)
}
// Try other detections, for example for SQLite (we don't want to enforce CGO here!)
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
return errors.Wrap(ErrUniqueViolation, err.Error())
}
return errors.WithStack(err)
}