@@ -11,6 +11,7 @@ import (
1111//
1212// https://sqlite.org/c3ref/errcode.html
1313type Error struct {
14+ sys error
1415 msg string
1516 sql string
1617 code res_t
@@ -33,7 +34,7 @@ func (e *Error) ExtendedCode() ExtendedErrorCode {
3334// Error implements the error interface.
3435func (e * Error ) Error () string {
3536 var b strings.Builder
36- b .WriteString (util .ErrorCodeString (uint32 ( e .code ) ))
37+ b .WriteString (util .ErrorCodeString (e .code ))
3738
3839 if e .msg != "" {
3940 b .WriteString (": " )
@@ -43,6 +44,14 @@ func (e *Error) Error() string {
4344 return b .String ()
4445}
4546
47+ // Unwrap returns the underlying operating system error
48+ // that caused the I/O error or failure to open a file.
49+ //
50+ // https://sqlite.org/c3ref/system_errno.html
51+ func (e * Error ) Unwrap () error {
52+ return e .sys
53+ }
54+
4655// Is tests whether this error matches a given [ErrorCode] or [ExtendedErrorCode].
4756//
4857// It makes it possible to do:
@@ -90,7 +99,16 @@ func (e *Error) SQL() string {
9099
91100// Error implements the error interface.
92101func (e ErrorCode ) Error () string {
93- return util .ErrorCodeString (uint32 (e ))
102+ return util .ErrorCodeString (e )
103+ }
104+
105+ // As converts this error to an [ExtendedErrorCode].
106+ func (e ErrorCode ) As (err any ) bool {
107+ c , ok := err .(* xErrorCode )
108+ if ok {
109+ * c = xErrorCode (e )
110+ }
111+ return ok
94112}
95113
96114// Temporary returns true for [BUSY] errors.
@@ -105,7 +123,7 @@ func (e ErrorCode) ExtendedCode() ExtendedErrorCode {
105123
106124// Error implements the error interface.
107125func (e ExtendedErrorCode ) Error () string {
108- return util .ErrorCodeString (uint32 ( e ) )
126+ return util .ErrorCodeString (e )
109127}
110128
111129// Is tests whether this error matches a given [ErrorCode].
@@ -150,14 +168,10 @@ func errorCode(err error, def ErrorCode) (msg string, code res_t) {
150168 return code .msg , res_t (code .code )
151169 }
152170
153- var ecode ErrorCode
154171 var xcode xErrorCode
155- switch {
156- case errors .As (err , & xcode ):
172+ if errors .As (err , & xcode ) {
157173 code = res_t (xcode )
158- case errors .As (err , & ecode ):
159- code = res_t (ecode )
160- default :
174+ } else {
161175 code = res_t (def )
162176 }
163177 return err .Error (), code
0 commit comments