mirror of https://github.com/status-im/go-waku.git
chore: separate error and success responses as per the specification, for mobile api (#324)
This commit is contained in:
parent
0da70116b0
commit
2881d0cd5e
|
@ -2,8 +2,11 @@ package gowaku
|
||||||
|
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
|
|
||||||
type jsonResponse struct {
|
type jsonResponseError struct {
|
||||||
Error *string `json:"error,omitempty"`
|
Error *string `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsonResponseSuccess struct {
|
||||||
Result interface{} `json:"result"`
|
Result interface{} `json:"result"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,14 +14,14 @@ func prepareJSONResponse(result interface{}, err error) string {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errStr := err.Error()
|
errStr := err.Error()
|
||||||
errResponse := jsonResponse{
|
errResponse := jsonResponseError{
|
||||||
Error: &errStr,
|
Error: &errStr,
|
||||||
}
|
}
|
||||||
response, _ := json.Marshal(&errResponse)
|
response, _ := json.Marshal(&errResponse)
|
||||||
return string(response)
|
return string(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := json.Marshal(jsonResponse{Result: result})
|
data, err := json.Marshal(jsonResponseSuccess{Result: result})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return prepareJSONResponse(nil, err)
|
return prepareJSONResponse(nil, err)
|
||||||
}
|
}
|
||||||
|
@ -26,17 +29,14 @@ func prepareJSONResponse(result interface{}, err error) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeJSONResponse(err error) string {
|
func makeJSONResponse(err error) string {
|
||||||
var errString *string = nil
|
|
||||||
result := true
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errStr := err.Error()
|
errStr := err.Error()
|
||||||
errString = &errStr
|
outBytes, _ := json.Marshal(jsonResponseError{Error: &errStr})
|
||||||
result = false
|
return string(outBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
out := jsonResponse{
|
out := jsonResponseSuccess{
|
||||||
Error: errString,
|
Result: true,
|
||||||
Result: result,
|
|
||||||
}
|
}
|
||||||
outBytes, _ := json.Marshal(out)
|
outBytes, _ := json.Marshal(out)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue