-
Notifications
You must be signed in to change notification settings - Fork 14
/
error.go
95 lines (83 loc) · 2.46 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
package xmlsec
import (
"fmt"
"runtime"
"strings"
"github.com/crewjam/errset"
)
// void captureXmlErrors();
import "C"
var globalErrors = map[uintptr]errset.ErrSet{}
type libraryError struct {
FileName string
Line int
FuncName string
Object string
Subject string
Reason int
Message string
}
func (e libraryError) Error() string {
return fmt.Sprintf(
"func=%s:file=%s:line=%d:obj=%s:subj=%s:error=%d:%s",
e.FuncName,
e.FileName,
e.Line,
e.Object,
e.Subject,
e.Reason,
e.Message)
}
//export onXmlsecError
func onXmlsecError(file *C.char, line C.int, funcName *C.char, errorObject *C.char, errorSubject *C.char, reason C.int, msg *C.char) {
err := libraryError{
FuncName: C.GoString(funcName),
FileName: C.GoString(file),
Line: int(line),
Object: C.GoString(errorObject),
Subject: C.GoString(errorSubject),
Reason: int(reason),
Message: C.GoString(msg)}
threadID := getThreadID()
globalErrors[threadID] = append(globalErrors[threadID], err)
}
//export onXmlError
func onXmlError(msg *C.char) {
threadID := getThreadID()
globalErrors[threadID] = append(globalErrors[threadID],
fmt.Errorf("%s", strings.TrimSuffix(C.GoString(msg), "\n")))
}
// startProcessingXML is called whenever we enter a function exported by this package.
// It locks the current goroutine to the current thread and establishes a thread-local
// error object. If the library later calls onError then the error will be appended
// to the error object associated with the current thread.
func startProcessingXML() {
runtime.LockOSThread()
globalErrors[getThreadID()] = errset.ErrSet{}
C.captureXmlErrors()
}
// stopProcessingXML unlocks the goroutine-thread lock and deletes the current
// error stack.
func stopProcessingXML() {
delete(globalErrors, getThreadID())
runtime.UnlockOSThread()
}
// popError returns the global error for the current thread and resets it to
// an empty error. Returns nil if no errors have occurred. This function must be
// called after startProcessingXML() and before stopProcessingXML(). All three
// functions must be called on the same goroutine.
func popError() error {
threadID := getThreadID()
rv := globalErrors[threadID].ReturnValue()
globalErrors[threadID] = errset.ErrSet{}
return rv
}
// mustPopError is like popError except that if there is no error on the stack
// it returns a generic error.
func mustPopError() error {
err := popError()
if err == nil {
err = fmt.Errorf("libxmlsec: call failed")
}
return err
}