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