forked from mozilla/masche
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cresponse.go
57 lines (47 loc) · 1.5 KB
/
cresponse.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
package cresponse
// #include "response.h"
// #cgo CFLAGS: -std=c99
import "C"
import (
"fmt"
"reflect"
"unsafe"
)
// CError is the Go represnentation of response.h's error_t.
type CError struct {
number int
description string
}
func (err CError) Error() string {
return fmt.Sprintf("System error number %d: %s", err.number, err.description)
}
// GetResponsesErrors returns the Go representation of the errors present in a C.response_t.
//
// NOTE: cgo types are private to each module, so exporting a function that expects a *C.response_t doesn't make sense,
// so we export a function with an unsafe.Pointer and we cast it internally.
func GetResponsesErrors(responsePointer unsafe.Pointer) (softerrors []error, harderror error) {
response := (*C.response_t)(responsePointer)
if response.fatal_error != nil && int(response.fatal_error.error_number) != 0 {
harderror = cErrorFromErrorT(*response.fatal_error)
} else {
harderror = nil
}
softerrorsCount := int(response.soft_errors_count)
softerrors = make([]error, 0, softerrorsCount)
cSoftErrorsHeader := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(response.soft_errors)),
Len: softerrorsCount,
Cap: softerrorsCount,
}
cSoftErrors := *(*[]C.error_t)(unsafe.Pointer(&cSoftErrorsHeader))
for _, cErr := range cSoftErrors {
softerrors = append(softerrors, cErrorFromErrorT(cErr))
}
return
}
func cErrorFromErrorT(err C.error_t) CError {
return CError{
number: int(err.error_number),
description: C.GoString(err.description),
}
}