go-waku/library/response.go

49 lines
917 B
Go

package main
import "C"
import (
"encoding/json"
)
var errToCodeMap = map[error]int{
//transactions.ErrInvalidTxSender: codeErrInvalidTxSender,
}
type JSONResponse struct {
Error *string `json:"error,omitempty"`
Result interface{} `json:"result"`
}
func prepareJSONResponse(result interface{}, err error) *C.char {
if err != nil {
errStr := err.Error()
errResponse := JSONResponse{
Error: &errStr,
}
response, _ := json.Marshal(&errResponse)
return C.CString(string(response))
}
data, err := json.Marshal(JSONResponse{Result: result})
if err != nil {
return prepareJSONResponse(nil, err)
}
return C.CString(string(data))
}
func makeJSONResponse(err error) *C.char {
var errString *string = nil
if err != nil {
errStr := err.Error()
errString = &errStr
}
out := JSONResponse{
Error: errString,
}
outBytes, _ := json.Marshal(out)
return C.CString(string(outBytes))
}