-
Notifications
You must be signed in to change notification settings - Fork 37
/
error.go
99 lines (89 loc) · 2.66 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package go_ibm_db
import (
"database/sql/driver"
"fmt"
"runtime"
"strings"
"unsafe"
"github.com/ibmdb/go_ibm_db/api"
trc "github.com/ibmdb/go_ibm_db/log2"
)
func IsError(ret api.SQLRETURN) bool {
trc.Trace1("error.go: IsError() - ENTRY")
if ret == api.SQL_SUCCESS {
trc.Trace1("api.SQL_SUCCESS")
} else if ret == api.SQL_SUCCESS_WITH_INFO {
trc.Trace1("api.SQL_SUCCESS_WITH_INFO")
}
trc.Trace1("error.go: IsError() - EXIT")
return !(ret == api.SQL_SUCCESS || ret == api.SQL_SUCCESS_WITH_INFO)
}
type DiagRecord struct {
State string
NativeError int
Message string
}
func (r *DiagRecord) String() string {
return fmt.Sprintf("{%s} %s", r.State, r.Message)
}
type Error struct {
APIName string
Diag []DiagRecord
}
func (e *Error) Error() string {
trc.Trace1("error.go: Error() - ENTRY")
ss := make([]string, len(e.Diag))
for i, r := range e.Diag {
ss[i] = r.String()
}
trc.Trace1(fmt.Sprintf("%s : %s", e.APIName, ss))
trc.Trace1("error.go: Error() - EXIT")
return e.APIName + ": " + strings.Join(ss, "\n")
}
func NewError(apiName string, handle interface{}) error {
trc.Trace1("error.go: NewError() - ENTRY")
trc.Trace1(fmt.Sprintf("apiName=%s",apiName))
var ret api.SQLRETURN
h, ht := ToHandleAndType(handle)
err := &Error{APIName: apiName}
var ne api.SQLINTEGER
state := make([]uint16, 6)
msg := make([]uint16, api.SQL_MAX_MESSAGE_LENGTH)
for i := 1; ; i++ {
if runtime.GOOS == "zos" {
ret = api.SQLGetDiagRec(ht, h, api.SQLSMALLINT(i),
(*api.SQLWCHAR)(unsafe.Pointer(&state[0])), &ne,
(*api.SQLWCHAR)(unsafe.Pointer(&msg[0])),
api.SQLSMALLINT(2*len(msg)), nil) // odbc api on zos doesn't handle null terminated strings, the exact size is passed
} else {
ret = api.SQLGetDiagRec(ht, h, api.SQLSMALLINT(i),
(*api.SQLWCHAR)(unsafe.Pointer(&state[0])), &ne,
(*api.SQLWCHAR)(unsafe.Pointer(&msg[0])),
api.SQLSMALLINT(len(msg)), nil)
}
if ret == api.SQL_NO_DATA {
break
}
if IsError(ret) {
trc.Trace1(fmt.Sprintf("SQLGetDiagRec failed: ret=%d", ret))
panic(fmt.Errorf("SQLGetDiagRec failed: ret=%d", ret))
}
r := DiagRecord{
State: api.UTF16ToString(state),
NativeError: int(ne),
Message: api.UTF16ToString(msg),
}
if strings.Contains(r.Message, "CLI0106E") ||
strings.Contains(r.Message, "CLI0107E") ||
strings.Contains(r.Message, "CLI0108E") {
return driver.ErrBadConn
}
err.Diag = append(err.Diag, r)
}
trc.Trace1(fmt.Sprintf("Error: %s", err))
trc.Trace1("error.go: NewError() - EXIT")
return err
}