2022-04-12 12:12:14 +00:00
|
|
|
package gowaku
|
|
|
|
|
|
|
|
import "encoding/json"
|
|
|
|
|
|
|
|
type jsonResponse struct {
|
|
|
|
Error *string `json:"error,omitempty"`
|
|
|
|
Result interface{} `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareJSONResponse(result interface{}, err error) string {
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errStr := err.Error()
|
|
|
|
errResponse := jsonResponse{
|
|
|
|
Error: &errStr,
|
|
|
|
}
|
|
|
|
response, _ := json.Marshal(&errResponse)
|
|
|
|
return string(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(jsonResponse{Result: result})
|
|
|
|
if err != nil {
|
|
|
|
return prepareJSONResponse(nil, err)
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeJSONResponse(err error) string {
|
|
|
|
var errString *string = nil
|
2022-08-09 13:48:23 +00:00
|
|
|
result := true
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
|
|
|
errStr := err.Error()
|
|
|
|
errString = &errStr
|
2022-08-09 13:48:23 +00:00
|
|
|
result = false
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out := jsonResponse{
|
2022-08-09 13:48:23 +00:00
|
|
|
Error: errString,
|
|
|
|
Result: result,
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(out)
|
|
|
|
|
|
|
|
return string(outBytes)
|
|
|
|
}
|