package sqlite3
import (
"errors"
"strings"
"github.com/ncruces/go-sqlite3/internal/util"
)
type Error struct {
msg string
sql string
code res_t
}
func (e *Error ) Code () ErrorCode {
return ErrorCode (e .code )
}
func (e *Error ) ExtendedCode () ExtendedErrorCode {
return xErrorCode (e .code )
}
func (e *Error ) Error () string {
var b strings .Builder
b .WriteString (util .ErrorCodeString (uint32 (e .code )))
if e .msg != "" {
b .WriteString (": " )
b .WriteString (e .msg )
}
return b .String ()
}
func (e *Error ) Is (err error ) bool {
switch c := err .(type ) {
case ErrorCode :
return c == e .Code ()
case ExtendedErrorCode :
return c == e .ExtendedCode ()
}
return false
}
func (e *Error ) As (err any ) bool {
switch c := err .(type ) {
case *ErrorCode :
*c = e .Code ()
return true
case *ExtendedErrorCode :
*c = e .ExtendedCode ()
return true
}
return false
}
func (e *Error ) Temporary () bool {
return e .Code () == BUSY || e .Code () == INTERRUPT
}
func (e *Error ) Timeout () bool {
return e .ExtendedCode () == BUSY_TIMEOUT
}
func (e *Error ) SQL () string {
return e .sql
}
func (e ErrorCode ) Error () string {
return util .ErrorCodeString (uint32 (e ))
}
func (e ErrorCode ) Temporary () bool {
return e == BUSY || e == INTERRUPT
}
func (e ErrorCode ) ExtendedCode () ExtendedErrorCode {
return xErrorCode (e )
}
func (e ExtendedErrorCode ) Error () string {
return util .ErrorCodeString (uint32 (e ))
}
func (e ExtendedErrorCode ) Is (err error ) bool {
c , ok := err .(ErrorCode )
return ok && c == ErrorCode (e )
}
func (e ExtendedErrorCode ) As (err any ) bool {
c , ok := err .(*ErrorCode )
if ok {
*c = ErrorCode (e )
}
return ok
}
func (e ExtendedErrorCode ) Temporary () bool {
return ErrorCode (e ) == BUSY || ErrorCode (e ) == INTERRUPT
}
func (e ExtendedErrorCode ) Timeout () bool {
return e == BUSY_TIMEOUT
}
func (e ExtendedErrorCode ) Code () ErrorCode {
return ErrorCode (e )
}
func errorCode(err error , def ErrorCode ) (msg string , code res_t ) {
switch code := err .(type ) {
case nil :
return "" , _OK
case ErrorCode :
return "" , res_t (code )
case xErrorCode :
return "" , res_t (code )
case *Error :
return code .msg , res_t (code .code )
}
var ecode ErrorCode
var xcode xErrorCode
switch {
case errors .As (err , &xcode ):
code = res_t (xcode )
case errors .As (err , &ecode ):
code = res_t (ecode )
default :
code = res_t (def )
}
return err .Error(), code
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .