2023-01-12 13:43:35 +00:00
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
|
|
import (
|
2023-02-07 03:11:59 +00:00
|
|
|
"runtime"
|
2023-01-12 13:43:35 +00:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-09 16:40:48 +00:00
|
|
|
// Error wraps an SQLite Error Code.
|
|
|
|
|
//
|
|
|
|
|
// https://www.sqlite.org/c3ref/errcode.html
|
2023-01-12 13:43:35 +00:00
|
|
|
type Error struct {
|
2023-02-09 16:40:48 +00:00
|
|
|
code uint64
|
|
|
|
|
str string
|
|
|
|
|
msg string
|
2023-02-10 16:42:49 +00:00
|
|
|
sql string
|
2023-01-12 13:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:40:48 +00:00
|
|
|
// Code returns the primary error code for this error.
|
|
|
|
|
//
|
|
|
|
|
// https://www.sqlite.org/rescode.html
|
|
|
|
|
func (e *Error) Code() ErrorCode {
|
|
|
|
|
return ErrorCode(e.code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExtendedCode returns the extended error code for this error.
|
|
|
|
|
//
|
|
|
|
|
// https://www.sqlite.org/rescode.html
|
|
|
|
|
func (e *Error) ExtendedCode() ExtendedErrorCode {
|
|
|
|
|
return ExtendedErrorCode(e.code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Error implements the error interface.
|
2023-01-21 00:33:46 +00:00
|
|
|
func (e *Error) Error() string {
|
2023-01-12 13:43:35 +00:00
|
|
|
var b strings.Builder
|
|
|
|
|
b.WriteString("sqlite3: ")
|
|
|
|
|
|
|
|
|
|
if e.str != "" {
|
|
|
|
|
b.WriteString(e.str)
|
|
|
|
|
} else {
|
2023-02-09 16:40:48 +00:00
|
|
|
b.WriteString(strconv.Itoa(int(e.code)))
|
2023-01-12 13:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if e.msg != "" {
|
|
|
|
|
b.WriteByte(':')
|
|
|
|
|
b.WriteByte(' ')
|
|
|
|
|
b.WriteString(e.msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
|
}
|
2023-01-19 14:31:32 +00:00
|
|
|
|
2023-02-20 13:38:03 +00:00
|
|
|
// Temporary returns true for [BUSY] errors.
|
|
|
|
|
func (e *Error) Temporary() bool {
|
|
|
|
|
return e.Code() == BUSY
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 16:42:49 +00:00
|
|
|
// SQL returns the SQL starting at the token that triggered a syntax error.
|
|
|
|
|
func (e *Error) SQL() string {
|
|
|
|
|
return e.sql
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-19 14:31:32 +00:00
|
|
|
type errorString string
|
|
|
|
|
|
|
|
|
|
func (e errorString) Error() string { return string(e) }
|
|
|
|
|
|
|
|
|
|
const (
|
2023-01-25 14:59:02 +00:00
|
|
|
nilErr = errorString("sqlite3: invalid memory address or null pointer dereference")
|
2023-01-19 14:31:32 +00:00
|
|
|
oomErr = errorString("sqlite3: out of memory")
|
|
|
|
|
rangeErr = errorString("sqlite3: index out of range")
|
|
|
|
|
noNulErr = errorString("sqlite3: missing NUL terminator")
|
|
|
|
|
noGlobalErr = errorString("sqlite3: could not find global: ")
|
|
|
|
|
noFuncErr = errorString("sqlite3: could not find function: ")
|
2023-02-25 01:29:46 +00:00
|
|
|
binaryErr = errorString("sqlite3: no SQLite binary embed/set/loaded")
|
2023-02-21 04:30:24 +00:00
|
|
|
timeErr = errorString("sqlite3: invalid time value")
|
2023-02-25 01:29:46 +00:00
|
|
|
tailErr = errorString("sqlite3: non-empty tail")
|
2023-02-23 13:29:51 +00:00
|
|
|
notImplErr = errorString("sqlite3: not implemented")
|
2023-01-19 14:31:32 +00:00
|
|
|
)
|
2023-02-07 03:11:59 +00:00
|
|
|
|
|
|
|
|
func assertErr() errorString {
|
|
|
|
|
msg := "sqlite3: assertion failed"
|
|
|
|
|
if _, file, line, ok := runtime.Caller(1); ok {
|
|
|
|
|
msg += " (" + file + ":" + strconv.Itoa(line) + ")"
|
|
|
|
|
}
|
|
|
|
|
return errorString(msg)
|
|
|
|
|
}
|