go-waku/library/mobile/response.go

45 lines
853 B
Go
Raw Normal View History

package gowaku
import "encoding/json"
type jsonResponseError struct {
Error *string `json:"error"`
}
type jsonResponseSuccess struct {
Result interface{} `json:"result"`
}
2023-08-10 13:30:38 +00:00
func prepareJSONResponse(result interface{}, err error) string {
if err != nil {
errStr := err.Error()
errResponse := jsonResponseError{
Error: &errStr,
}
response, _ := json.Marshal(&errResponse)
return string(response)
}
data, err := json.Marshal(jsonResponseSuccess{Result: result})
if err != nil {
2023-08-10 13:30:38 +00:00
return prepareJSONResponse(nil, err)
}
return string(data)
}
2023-08-10 13:30:38 +00:00
func makeJSONResponse(err error) string {
if err != nil {
errStr := err.Error()
outBytes, _ := json.Marshal(jsonResponseError{Error: &errStr})
return string(outBytes)
}
out := jsonResponseSuccess{
Result: true,
}
outBytes, _ := json.Marshal(out)
return string(outBytes)
}