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"`
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:08:46 +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 {
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(nil, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:08:46 +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)
|
|
|
|
}
|