-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patherror.go
61 lines (52 loc) · 1.32 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package mysql
/*
#include "cgo.h"
*/
import "C"
import "fmt"
// SQL error.
type SqlError struct {
Num int
Message string
Query string
}
// Get error string.
func (se *SqlError) Error() string {
if se.Query == "" {
return fmt.Sprintf("%v (errno %v)", se.Message, se.Num)
}
return fmt.Sprintf("%v (errno %v) during query: %s", se.Message, se.Num, se.Query)
}
// Get error number.
func (se *SqlError) Number() int {
return se.Num
}
func (conn *Connection) lastError(query string) error {
if err := C.my_error(&conn.c); *err != 0 {
return &SqlError{Num: int(C.my_errno(&conn.c)), Message: C.GoString(err), Query: query}
}
return &SqlError{0, "Unknow", string(query)}
}
// Statement error.
type StmtError struct {
Num int
Message string
Stmt *Stmt
}
// Get error string.
func (self *StmtError) Error() string {
if len(self.Stmt.sql) == 0 {
return fmt.Sprintf("%v (errno %v)", self.Message, self.Num)
}
return fmt.Sprintf("%v (errno %v) during query: %s", self.Message, self.Num, self.Stmt.sql)
}
// Get error number.
func (self *StmtError) Number() int {
return self.Num
}
func (stmt *Stmt) lastError() error {
if err := C.my_stmt_error(stmt.s); *err != 0 {
return &StmtError{Num: int(C.my_stmt_errno(stmt.s)), Message: C.GoString(C.my_stmt_error(stmt.s)), Stmt: stmt}
}
return &StmtError{0, "Unknow", stmt}
}