forked from rai-project/go-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
64 lines (53 loc) · 930 Bytes
/
errors.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
package pytorch
// #include "cbits/predictor.hpp"
// #include <stdlib.h>
//
import "C"
import (
"unsafe"
"github.com/pkg/errors"
)
// Error errors returned by torch functions
type Error struct {
message string
}
func (te *Error) Error() string {
return te.message
}
func checkError(err C.Torch_Error) *Error {
if err.message != nil {
defer C.free(unsafe.Pointer(err.message))
return &Error{
message: C.GoString(err.message),
}
}
return nil
}
func HasError() bool {
return int(C.Torch_HasError()) == 1
}
func GetErrorString() string {
msg := C.Torch_GetErrorString()
if msg == nil {
return ""
}
return C.GoString(msg)
}
func GetError() error {
if !HasError() {
return nil
}
err := errors.New(GetErrorString())
ResetError()
return err
}
func ResetError() {
C.Torch_ResetError()
}
func PanicOnError() {
msg := C.Torch_GetErrorString()
if msg == nil {
return
}
panic(C.GoString(msg))
}