multitenantStack/controllers/base.go

59 lines
1.5 KiB
Go
Raw Normal View History

2018-11-07 10:10:51 +00:00
package controllers
import (
2018-11-13 20:39:04 +00:00
"fmt"
2018-11-07 10:10:51 +00:00
"github.com/astaxie/beego"
)
// JSONBasicResponse The minimal JSON response
type JSONBasicResponse struct {
2018-11-07 10:10:51 +00:00
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
2018-11-07 10:10:51 +00:00
// 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}
2018-11-07 15:27:39 +00:00
c.Data["json"] = &json
///c.Ctx.ResponseWriter.WriteHeader(400)
c.ServeJSON()
2018-11-07 10:10:51 +00:00
}
// ServeJSONErrorWithCode respond with a JSON error and specify code
func (c *BaseController) ServeJSONErrorWithCode(errorcode int, message string) {
json := JSONBasicResponse{errorcode, message}
2018-11-07 15:27:39 +00:00
c.Data["json"] = &json
c.ServeJSON()
}
2018-11-13 20:39:04 +00:00
// 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}
2018-11-07 15:27:39 +00:00
c.Data["json"] = &json
c.ServeJSON()
2018-11-07 10:10:51 +00:00
}