Skip to content

Commit 41dc46a

Browse files
committed
Optimize errors a bit.
1 parent e5c285b commit 41dc46a

File tree

5 files changed

+28
-37
lines changed

5 files changed

+28
-37
lines changed

error.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package sqlite3
22

33
import (
44
"errors"
5-
"strconv"
65
"strings"
76

87
"github.com/ncruces/go-sqlite3/internal/util"
@@ -12,7 +11,6 @@ import (
1211
//
1312
// https://sqlite.org/c3ref/errcode.html
1413
type Error struct {
15-
str string
1614
msg string
1715
sql string
1816
code res_t
@@ -29,19 +27,13 @@ func (e *Error) Code() ErrorCode {
2927
//
3028
// https://sqlite.org/rescode.html
3129
func (e *Error) ExtendedCode() ExtendedErrorCode {
32-
return ExtendedErrorCode(e.code)
30+
return xErrorCode(e.code)
3331
}
3432

3533
// Error implements the error interface.
3634
func (e *Error) Error() string {
3735
var b strings.Builder
38-
b.WriteString("sqlite3: ")
39-
40-
if e.str != "" {
41-
b.WriteString(e.str)
42-
} else {
43-
b.WriteString(strconv.Itoa(int(e.code)))
44-
}
36+
b.WriteString(util.ErrorCodeString(uint32(e.code)))
4537

4638
if e.msg != "" {
4739
b.WriteString(": ")
@@ -103,12 +95,12 @@ func (e ErrorCode) Error() string {
10395

10496
// Temporary returns true for [BUSY] errors.
10597
func (e ErrorCode) Temporary() bool {
106-
return e == BUSY
98+
return e == BUSY || e == INTERRUPT
10799
}
108100

109101
// ExtendedCode returns the extended error code for this error.
110102
func (e ErrorCode) ExtendedCode() ExtendedErrorCode {
111-
return ExtendedErrorCode(e)
103+
return xErrorCode(e)
112104
}
113105

114106
// Error implements the error interface.
@@ -133,7 +125,7 @@ func (e ExtendedErrorCode) As(err any) bool {
133125

134126
// Temporary returns true for [BUSY] errors.
135127
func (e ExtendedErrorCode) Temporary() bool {
136-
return ErrorCode(e) == BUSY
128+
return ErrorCode(e) == BUSY || ErrorCode(e) == INTERRUPT
137129
}
138130

139131
// Timeout returns true for [BUSY_TIMEOUT] errors.

error_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestError(t *testing.T) {
4343
if !errors.Is(err, xErrorCode(0x8080)) {
4444
t.Errorf("want true")
4545
}
46-
if s := err.Error(); s != "sqlite3: 32896" {
46+
if s := err.Error(); s != "sqlite3: unknown error" {
4747
t.Errorf("got %q", s)
4848
}
4949
if ok := errors.As(err.ExtendedCode(), &ecode); !ok || ecode != ErrorCode(0x80) {
@@ -83,7 +83,7 @@ func TestError_Temporary(t *testing.T) {
8383
}
8484
}
8585
{
86-
err := ExtendedErrorCode(tt.code)
86+
err := xErrorCode(tt.code)
8787
if got := err.Temporary(); got != tt.want {
8888
t.Errorf("ExtendedErrorCode.Temporary(%d) = %v, want %v", tt.code, got, tt.want)
8989
}
@@ -115,7 +115,7 @@ func TestError_Timeout(t *testing.T) {
115115
}
116116
}
117117
{
118-
err := ExtendedErrorCode(tt.code)
118+
err := xErrorCode(tt.code)
119119
if got := err.Timeout(); got != tt.want {
120120
t.Errorf("Error.Timeout(%d) = %v, want %v", tt.code, got, tt.want)
121121
}
@@ -156,12 +156,12 @@ func Test_ExtendedErrorCode_Error(t *testing.T) {
156156
defer db.Close()
157157

158158
// Test all extended error codes.
159-
for i := 0; i == int(ExtendedErrorCode(i)); i++ {
159+
for i := 0; i == int(xErrorCode(i)); i++ {
160160
want := "sqlite3: "
161161
ptr := ptr_t(db.call("sqlite3_errstr", stk_t(i)))
162162
want += util.ReadString(db.mod, ptr, _MAX_NAME)
163163

164-
got := ExtendedErrorCode(i).Error()
164+
got := xErrorCode(i).Error()
165165
if got != want {
166166
t.Fatalf("got %q, want %q, with %d", got, want, i)
167167
}

internal/util/error.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func ErrorCodeString(rc uint32) string {
7575
return "sqlite3: unable to open database file"
7676
case PROTOCOL:
7777
return "sqlite3: locking protocol"
78-
case FORMAT:
78+
case EMPTY:
7979
break
8080
case SCHEMA:
8181
return "sqlite3: database schema has changed"
@@ -91,7 +91,7 @@ func ErrorCodeString(rc uint32) string {
9191
break
9292
case AUTH:
9393
return "sqlite3: authorization denied"
94-
case EMPTY:
94+
case FORMAT:
9595
break
9696
case RANGE:
9797
return "sqlite3: column index out of range"

internal/util/mem.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,10 @@ func ReadString(mod api.Module, ptr Ptr_t, maxlen int64) string {
135135
panic(RangeErr)
136136
}
137137
}
138-
if i := bytes.IndexByte(buf, 0); i < 0 {
139-
panic(NoNulErr)
140-
} else {
138+
if i := bytes.IndexByte(buf, 0); i >= 0 {
141139
return string(buf[:i])
142140
}
141+
panic(NoNulErr)
143142
}
144143

145144
func WriteBytes(mod api.Module, ptr Ptr_t, b []byte) {

sqlite.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,33 +120,33 @@ func (sqlt *sqlite) error(rc res_t, handle ptr_t, sql ...string) error {
120120
return nil
121121
}
122122

123-
err := Error{code: rc}
124-
125-
if err.Code() == NOMEM || err.ExtendedCode() == IOERR_NOMEM {
123+
if ErrorCode(rc) == NOMEM || xErrorCode(rc) == IOERR_NOMEM {
126124
panic(util.OOMErr)
127125
}
128126

129-
if ptr := ptr_t(sqlt.call("sqlite3_errstr", stk_t(rc))); ptr != 0 {
130-
err.str = util.ReadString(sqlt.mod, ptr, _MAX_NAME)
131-
}
132-
133127
if handle != 0 {
128+
var msg, query string
134129
if ptr := ptr_t(sqlt.call("sqlite3_errmsg", stk_t(handle))); ptr != 0 {
135-
err.msg = util.ReadString(sqlt.mod, ptr, _MAX_LENGTH)
130+
msg = util.ReadString(sqlt.mod, ptr, _MAX_LENGTH)
131+
switch {
132+
case msg == "not an error":
133+
msg = ""
134+
case msg == util.ErrorCodeString(uint32(rc))[len("sqlite3: "):]:
135+
msg = ""
136+
}
136137
}
137138

138139
if len(sql) != 0 {
139140
if i := int32(sqlt.call("sqlite3_error_offset", stk_t(handle))); i != -1 {
140-
err.sql = sql[0][i:]
141+
query = sql[0][i:]
141142
}
142143
}
143-
}
144144

145-
switch err.msg {
146-
case err.str, "not an error":
147-
err.msg = ""
145+
if msg != "" || query != "" {
146+
return &Error{code: rc, msg: msg, sql: query}
147+
}
148148
}
149-
return &err
149+
return xErrorCode(rc)
150150
}
151151

152152
func (sqlt *sqlite) getfn(name string) api.Function {

0 commit comments

Comments
 (0)