Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
201 lines (184 loc) · 6.54 KB
/
Copy patherrors.go
File metadata and controls
201 lines (184 loc) · 6.54 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package sqlite3wasm
import (
"context"
"fmt"
"strconv"
"strings"
"time"
)
// Error is an error that SQLite returned. A caller finds the code with
// errors.As, or tests the primary code with errors.Is and a Code:
//
// if errors.Is(err, sqlite3wasm.ErrConstraint) { ... }
type Error struct {
// Code is the primary result code, which is the low byte of the
// extended code.
Code Code
// ExtendedCode is the extended result code, such as 2067 for
// SQLITE_CONSTRAINT_UNIQUE.
ExtendedCode int
// Message is the text that SQLite gave.
Message string
}
// Error implements error.
func (e *Error) Error() string {
return fmt.Sprintf("sqlite3wasm: %s (%s)", e.Message, e.Code.Name())
}
// Is reports whether target is the primary code of e.
func (e *Error) Is(target error) bool {
c, ok := target.(Code)
return ok && c == e.Code
}
// AuthenticationRefused is false. SQLite has no credential to refuse. A
// client such as usql asks every error for this method.
func (*Error) AuthenticationRefused() bool { return false }
// ErrVerbose returns the error in the form of the verbose mode of libpq,
// which psql shows for \errverbose, so that the output is the same for every
// database. usql finds the method with errors.As. The code is the name of the
// primary code, and the DETAIL line holds the extended code. SQLite gives no
// position in the statement, so the argument is not used.
func (e *Error) ErrVerbose(string) string {
var b strings.Builder
b.WriteString("ERROR: " + e.Code.Name() + ": " + e.Message)
if e.ExtendedCode != int(e.Code) {
fmt.Fprintf(&b, "\nDETAIL: extended code %d", e.ExtendedCode)
}
return b.String()
}
// Code is a primary result code of SQLite. It is also an error, so that
// errors.Is can compare an Error with it.
type Code int
// The primary result codes that are errors. See
// https://sqlite.org/rescode.html.
const (
ErrError Code = 1
ErrInternal Code = 2
ErrPerm Code = 3
ErrAbort Code = 4
ErrBusy Code = 5
ErrLocked Code = 6
ErrNoMem Code = 7
ErrReadOnly Code = 8
ErrInterrupt Code = 9
ErrIO Code = 10
ErrCorrupt Code = 11
ErrNotFound Code = 12
ErrFull Code = 13
ErrCantOpen Code = 14
ErrProtocol Code = 15
ErrEmpty Code = 16
ErrSchema Code = 17
ErrTooBig Code = 18
ErrConstraint Code = 19
ErrMismatch Code = 20
ErrMisuse Code = 21
ErrNoLFS Code = 22
ErrAuth Code = 23
ErrFormat Code = 24
ErrRange Code = 25
ErrNotADB Code = 26
)
// codeNames are the names of the primary codes, without the SQLITE_ prefix.
var codeNames = map[Code]string{
ErrError: "ERROR", ErrInternal: "INTERNAL", ErrPerm: "PERM", ErrAbort: "ABORT",
ErrBusy: "BUSY", ErrLocked: "LOCKED", ErrNoMem: "NOMEM", ErrReadOnly: "READONLY",
ErrInterrupt: "INTERRUPT", ErrIO: "IOERR", ErrCorrupt: "CORRUPT",
ErrNotFound: "NOTFOUND", ErrFull: "FULL", ErrCantOpen: "CANTOPEN",
ErrProtocol: "PROTOCOL", ErrEmpty: "EMPTY", ErrSchema: "SCHEMA", ErrTooBig: "TOOBIG",
ErrConstraint: "CONSTRAINT", ErrMismatch: "MISMATCH", ErrMisuse: "MISUSE",
ErrNoLFS: "NOLFS", ErrAuth: "AUTH", ErrFormat: "FORMAT", ErrRange: "RANGE",
ErrNotADB: "NOTADB",
}
// Name returns the name of the code, such as SQLITE_CONSTRAINT.
func (c Code) Name() string {
if n, ok := codeNames[c]; ok {
return "SQLITE_" + n
}
return "SQLITE_" + strconv.Itoa(int(c))
}
// Error implements error.
func (c Code) Error() string {
return "sqlite3wasm: " + c.Name()
}
// DriverError is an error of the driver and not of SQLite.
type DriverError string
// Error implements error.
func (e DriverError) Error() string {
return string(e)
}
// The errors of the driver.
const (
// ErrClosed is the error of a call on a closed connector, connection,
// statement or result set.
ErrClosed DriverError = "sqlite3wasm: using a closed object"
// ErrModule is the error of a module that does not hold what the driver
// needs, or that trapped.
ErrModule DriverError = "sqlite3wasm: running the module"
// ErrNamed is the error of a named argument, which the driver does not
// take yet.
ErrNamed DriverError = "sqlite3wasm: binding a named argument is not supported"
// ErrUnknownKey is the error of a key in a data source name that the
// driver does not know.
ErrUnknownKey DriverError = "sqlite3wasm: unknown key"
// ErrArguments is the error of a count of arguments that does not match
// the parameters of the statements.
ErrArguments DriverError = "sqlite3wasm: wrong number of arguments"
// ErrArgumentType is the error of an argument of a type that the driver
// cannot bind.
ErrArgumentType DriverError = "sqlite3wasm: unsupported argument type"
// ErrPreloadName is the error of Preload with an empty name or
// ":memory:".
ErrPreloadName DriverError = "sqlite3wasm: preloading needs a name that connections share"
// ErrIsolation is the error of an isolation level other than the default
// or serializable, which is the only level SQLite has.
ErrIsolation DriverError = "sqlite3wasm: setting an isolation level other than serializable is not supported"
)
// lastError reads the error of db for the result code rc. mod.mu must be
// held. If ctx is done and SQLite stopped the statement, it returns the error
// of the context, as database/sql expects.
func (mod *module) lastError(ctx context.Context, db uint32, rc uint32) error {
if Code(rc&0xff) == ErrInterrupt {
if err := stopped(ctx); err != nil {
return err
}
}
e := &Error{Code: Code(rc & 0xff), ExtendedCode: int(rc)}
var ptr uint32
var err error
if db != 0 {
ptr, err = mod.call(ctx, fnErrmsg, db)
} else {
ptr, err = mod.call(ctx, fnErrstr, rc)
}
if err != nil {
return err
}
if e.Message, err = mod.cstring(ctx, ptr); err != nil {
return err
}
return e
}
// stopped returns the error of ctx, if it is done or its deadline passed.
// The module reads the wall clock itself, so it can stop a statement before
// the timer of the context fires, which reads the monotonic clock. A browser
// lets the two drift apart. So this compares the wall clock in milliseconds,
// as the module does.
func stopped(ctx context.Context) error {
if err := ctx.Err(); err != nil {
return err
}
if ms := deadlineMillis(ctx); ms != 0 && time.Now().UnixMilli() >= ms {
return context.DeadlineExceeded
}
return nil
}
// deadlineMillis returns the deadline of ctx as milliseconds since the Unix
// epoch, rounded up, so that the module never stops a statement before the
// deadline. It is 0 if ctx has no deadline.
func deadlineMillis(ctx context.Context) int64 {
d, ok := ctx.Deadline()
if !ok {
return 0
}
return max((d.UnixNano()+999_999)/1_000_000, 1)
}