Commit 900751f4 authored by gwenn's avatar gwenn Committed by GitHub

Merge pull request #14 from navytux/y/open-error

open: Fix error message
parents 3415a54c 0d47edb6
...@@ -40,10 +40,23 @@ type OpenError struct { ...@@ -40,10 +40,23 @@ type OpenError struct {
} }
func (e OpenError) Error() string { func (e OpenError) Error() string {
file := e.Filename
if file == "" {
file = "(temporary)"
}
s := fmt.Sprintf("%s: ", file)
codeErr := e.Code.Error()
if len(e.Msg) > 0 { if len(e.Msg) > 0 {
return fmt.Sprintf("%s (%s)", e.Msg, e.Code.Error()) s += e.Msg
// error code and msg are often redundant but not always
// (see sqlite3ErrorWithMsg usages in SQLite3 sources)
if codeErr != e.Msg {
s += fmt.Sprintf(" (%s)", codeErr)
}
} else {
s += codeErr
} }
return e.Code.Error() return s
} }
// ConnError is a wrapper for all SQLite connection related error. // ConnError is a wrapper for all SQLite connection related error.
...@@ -250,16 +263,17 @@ func OpenVfs(filename string, vfsname string, flags ...OpenFlag) (*Conn, error) ...@@ -250,16 +263,17 @@ func OpenVfs(filename string, vfsname string, flags ...OpenFlag) (*Conn, error)
} }
rv := C.sqlite3_open_v2(cname, &db, C.int(openFlags), vfs) rv := C.sqlite3_open_v2(cname, &db, C.int(openFlags), vfs)
if rv != C.SQLITE_OK { if rv != C.SQLITE_OK {
err := OpenError{
Code: Errno(rv),
Filename: filename,
}
if db != nil { // try to extract futher details from db... if db != nil { // try to extract futher details from db...
err := OpenError{Code: Errno(rv), err.ExtendedCode = int(C.sqlite3_extended_errcode(db))
ExtendedCode: int(C.sqlite3_extended_errcode(db)), err.Msg = C.GoString(C.sqlite3_errmsg(db))
Msg: C.GoString(C.sqlite3_errmsg(db)),
Filename: filename,
}
C.sqlite3_close(db) C.sqlite3_close(db)
return nil, err return nil, err
} }
return nil, Errno(rv) return nil, err
} }
if db == nil { if db == nil {
return nil, errors.New("sqlite succeeded without returning a database") return nil, errors.New("sqlite succeeded without returning a database")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment