package controllers import ( "fmt" "github.com/astaxie/beego" ) // JSONBasicResponse The minimal JSON response type JSONBasicResponse struct { Status int Message string } // JSONError code for a input error const JSONError int = 400 // JSONInternalError code for an internal error const JSONInternalError int = 500 // JSONSuccess code for a success const JSONSuccess int = 200 // BaseController operations for BaseController type BaseController struct { beego.Controller } // ServeJSONError respond with a JSON error func (c *BaseController) ServeJSONError(message string) { json := JSONBasicResponse{JSONError, message} c.Data["json"] = &json ///c.Ctx.ResponseWriter.WriteHeader(400) c.ServeJSON() } // ServeJSONErrorWithCode respond with a JSON error and specify code func (c *BaseController) ServeJSONErrorWithCode(errorcode int, message string) { json := JSONBasicResponse{errorcode, message} c.Data["json"] = &json c.ServeJSON() } // ServeJSONErrorWithError respond with a JSON error and print an error message func (c *BaseController) ServeJSONErrorWithError(message string, err error) { message = fmt.Sprintf("%s %s", message, err.Error()) json := JSONBasicResponse{JSONError, message} c.Data["json"] = &json ///c.Ctx.ResponseWriter.WriteHeader(400) c.ServeJSON() } // ServeJSONSuccess respond with a JSON success message func (c *BaseController) ServeJSONSuccess(message string) { json := JSONBasicResponse{JSONSuccess, message} c.Data["json"] = &json c.ServeJSON() }