• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ory / x / 13912274386

18 Mar 2025 12:01AM UTC coverage: 61.082% (-1.2%) from 62.237%
13912274386

push

github

web-flow
chore: bump and reduce dependencies (#845)

- bump a bunch of dependencies
- move dev dependencies to `go tools`
- add go.mod for non-consumable module

35 of 42 new or added lines in 6 files covered. (83.33%)

179 existing lines in 21 files now uncovered.

7088 of 11604 relevant lines covered (61.08%)

0.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/sqlcon/error.go
1
// Copyright © 2023 Ory Corp
2
// SPDX-License-Identifier: Apache-2.0
3

4
package sqlcon
5

6
import (
7
        "database/sql"
8
        "net/http"
9

10
        "google.golang.org/grpc/codes"
11

12
        "github.com/go-sql-driver/mysql"
13
        "github.com/jackc/pgconn"
14
        "github.com/lib/pq"
15
        "github.com/pkg/errors"
16

17
        "github.com/ory/herodot"
18
)
19

20
var (
21
        // ErrUniqueViolation is returned when^a SQL INSERT / UPDATE command returns a conflict.
22
        ErrUniqueViolation = &herodot.DefaultError{
23
                CodeField:     http.StatusConflict,
24
                GRPCCodeField: codes.AlreadyExists,
25
                StatusField:   http.StatusText(http.StatusConflict),
26
                ErrorField:    "Unable to insert or update resource because a resource with that value exists already",
27
        }
28
        // ErrNoRows is returned when a SQL SELECT statement returns no rows.
29
        ErrNoRows = &herodot.DefaultError{
30
                CodeField:     http.StatusNotFound,
31
                GRPCCodeField: codes.NotFound,
32
                StatusField:   http.StatusText(http.StatusNotFound),
33
                ErrorField:    "Unable to locate the resource",
34
        }
35
        // ErrConcurrentUpdate is returned when the database is unable to serialize access due to a concurrent update.
36
        ErrConcurrentUpdate = &herodot.DefaultError{
37
                CodeField:     http.StatusBadRequest,
38
                GRPCCodeField: codes.Aborted,
39
                StatusField:   http.StatusText(http.StatusBadRequest),
40
                ErrorField:    "Unable to serialize access due to a concurrent update in another session",
41
        }
42
        ErrNoSuchTable = &herodot.DefaultError{
43
                CodeField:     http.StatusInternalServerError,
44
                GRPCCodeField: codes.Internal,
45
                StatusField:   http.StatusText(http.StatusInternalServerError),
46
                ErrorField:    "Unable to locate the table",
47
        }
48
)
49

50
func handlePostgres(err error, sqlState string) error {
×
51
        switch sqlState {
×
52
        case "23505": // "unique_violation"
×
53
                return errors.WithStack(ErrUniqueViolation.WithWrap(err))
×
54
        case "40001", // "serialization_failure" in CRDB
NEW
55
                "CR000": // "serialization_failure"
×
56
                return errors.WithStack(ErrConcurrentUpdate.WithWrap(err))
×
57
        case "42P01": // "no such table"
×
58
                return errors.WithStack(ErrNoSuchTable.WithWrap(err))
×
59
        }
60
        return errors.WithStack(err)
×
61
}
62

63
type stater interface {
64
        SQLState() string
65
}
66

67
// HandleError returns the right sqlcon.Err* depending on the input error.
UNCOV
68
func HandleError(err error) error {
×
UNCOV
69
        if err == nil {
×
UNCOV
70
                return nil
×
UNCOV
71
        }
×
72

UNCOV
73
        var st stater
×
UNCOV
74
        if errors.Is(err, sql.ErrNoRows) {
×
UNCOV
75
                return errors.WithStack(ErrNoRows)
×
UNCOV
76
        } else if errors.As(err, &st) {
×
77
                return handlePostgres(err, st.SQLState())
×
78
        } else if e := new(pq.Error); errors.As(err, &e) {
×
79
                return handlePostgres(err, string(e.Code))
×
80
        } else if e := new(pgconn.PgError); errors.As(err, &e) {
×
81
                return handlePostgres(err, e.Code)
×
82
        } else if e := new(mysql.MySQLError); errors.As(err, &e) {
×
83
                switch e.Number {
×
84
                case 1062:
×
85
                        return errors.WithStack(ErrUniqueViolation.WithWrap(err))
×
86
                case 1146:
×
87
                        return errors.WithStack(ErrNoSuchTable.WithWrap(e))
×
88
                }
89
        }
90

91
        if err := handleSqlite(err); err != nil {
×
92
                return err
×
93
        }
×
94

95
        return errors.WithStack(err)
×
96
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc