37 lines
738 B
Go
37 lines
738 B
Go
package sys
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
func SuccessJsonResponse(w http.ResponseWriter, data interface{}) {
|
|
raw, err := json.Marshal(data)
|
|
if err != nil {
|
|
// @TODO generic error
|
|
http.Error(w, `{"type":"error", "code":"request_data_could_prepared_for_reply"}`, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Write(raw)
|
|
}
|
|
|
|
func ErrorJsonResponse(w http.ResponseWriter, code string, err error) {
|
|
raw, err := json.Marshal(struct {
|
|
Type string
|
|
Code string
|
|
Msg string
|
|
}{
|
|
Type: "error",
|
|
Code: code,
|
|
Msg: err.Error(),
|
|
})
|
|
if err != nil {
|
|
// @TODO generic error
|
|
http.Error(w, `{"type":"error", "code":"request_data_could_prepared_for_reply"}`, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Write(raw)
|
|
}
|