作者 yangfu

fix mybeego

package common
import "fmt"
import (
"fmt"
"encoding/json"
)
// Must panics if err is not nil.
func Must(err error) {
... ... @@ -22,4 +25,12 @@ func Error2(v interface{}, err error) error {
func LogF(format string,args interface{})string{
return fmt.Sprintf(format,args)
}
func AssertJson(object interface{})string{
json,err :=json.Marshal(object)
if err!=nil{
return ""
}
return string(json)
}
\ No newline at end of file
... ...
... ... @@ -7,13 +7,6 @@ type Error struct {
err error
}
func (e Error)Error()string{
if e.err==nil{
return ""
}
return e.err.Error()
}
func NewError(code int,e error)Error{
return Error{
Code:code,
... ... @@ -24,3 +17,14 @@ func NewError(code int,e error)Error{
func NewErrorWithMsg(code int,msg string)Error{
return NewError(code,fmt.Errorf(msg))
}
func (e Error)Error()string{
if e.err==nil{
return ""
}
return e.err.Error()
}
func(e Error)Unwrap()error{
return e.err
}
... ...
package common
import (
"errors"
"fmt"
"testing"
)
... ... @@ -12,4 +13,19 @@ func Test_Error(t *testing.T){
emsg :=NewErrorWithMsg(2,"some error")
t.Log(emsg,emsg.Code)
}
func Test_AssertError(t *testing.T){
var targetErr = NewError(1,fmt.Errorf("%v","some error"))
var e error =targetErr
if !errors.Is(e,targetErr){
t.Fatal("errors.Is not equal")
}
if errors.Unwrap(e) ==nil{
t.Fatal("errors.Unwrap not nil")
}
var commErr Error
if !errors.As(e,&commErr){
t.Fatal("errors.As error")
}
}
\ No newline at end of file
... ...
... ... @@ -33,17 +33,16 @@ func (this *BaseController) Options() {
func (this *BaseController) AllowCross() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
//this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "uid, token,jwt, deviceid, appid,Content-Type,Authorization,from")
this.Ctx.WriteString("")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
//this.Ctx.WriteString("")
}
func (this *BaseController) Prepare() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
if this.Ctx.Input.Method() == "OPTIONS" {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
//this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "uid, token,jwt, deviceid, appid,Content-Type,Authorization,from")
this.Ctx.WriteString("")
//this.Ctx.WriteString("")
return
}
... ... @@ -118,34 +117,3 @@ func (this *BaseController) Finish() {
log.Info(fmt.Sprintf("<====Send to uid(%d) client: %d byte\nRequestId:%s RspBodyData: %s", this.RequestHead.Uid, length,this.RequestHead.GetRequestId(), string(strByte)))
}
}
// BaseControllerCallBack
type BaseControllerCallBack struct {
beego.Controller
Query map[string]string
JSONBody map[string]interface{}
ByteBody []byte
}
func (this *BaseControllerCallBack) Prepare() {
this.Query = map[string]string{}
input := this.Input()
for k := range input {
this.Query[k] = input.Get(k)
}
if this.Ctx.Input.RequestBody != nil {
log.Info("RecvHead:", string(this.Ctx.Input.Header("Authorization")))
this.ByteBody = this.Ctx.Input.RequestBody
}
}
func (this *BaseControllerCallBack) Resp(msg *Message) {
this.Data["json"] = msg
this.ServeJSON()
}
func (this *BaseControllerCallBack) Finish() {
strByte, _ := json.Marshal(this.Data["json"])
log.Debug("<====Send to client:\n", string(strByte))
}
... ...
... ... @@ -64,4 +64,8 @@ func init() {
ErrnoMsg[4] = "您目前使用的版本过低,无法显示最新的相关内容,请使用最新版本。"
ErrnoMsg[5] = "描述包含敏感词,请重新编辑"
ErrnoMsg[6] ="重复提交,请稍后再试"
}
func SetMessage(code int,msg string){
ErrnoMsg[code] = msg
}
\ No newline at end of file
... ...