作者 yangfu

fix mybeego

1 package common 1 package common
2 2
3 -import "fmt" 3 +import (
  4 + "fmt"
  5 + "encoding/json"
  6 +)
4 7
5 // Must panics if err is not nil. 8 // Must panics if err is not nil.
6 func Must(err error) { 9 func Must(err error) {
@@ -23,3 +26,11 @@ func Error2(v interface{}, err error) error { @@ -23,3 +26,11 @@ func Error2(v interface{}, err error) error {
23 func LogF(format string,args interface{})string{ 26 func LogF(format string,args interface{})string{
24 return fmt.Sprintf(format,args) 27 return fmt.Sprintf(format,args)
25 } 28 }
  29 +
  30 +func AssertJson(object interface{})string{
  31 + json,err :=json.Marshal(object)
  32 + if err!=nil{
  33 + return ""
  34 + }
  35 + return string(json)
  36 +}
@@ -7,13 +7,6 @@ type Error struct { @@ -7,13 +7,6 @@ type Error struct {
7 err error 7 err error
8 } 8 }
9 9
10 -func (e Error)Error()string{  
11 - if e.err==nil{  
12 - return ""  
13 - }  
14 - return e.err.Error()  
15 -}  
16 -  
17 func NewError(code int,e error)Error{ 10 func NewError(code int,e error)Error{
18 return Error{ 11 return Error{
19 Code:code, 12 Code:code,
@@ -24,3 +17,14 @@ func NewError(code int,e error)Error{ @@ -24,3 +17,14 @@ func NewError(code int,e error)Error{
24 func NewErrorWithMsg(code int,msg string)Error{ 17 func NewErrorWithMsg(code int,msg string)Error{
25 return NewError(code,fmt.Errorf(msg)) 18 return NewError(code,fmt.Errorf(msg))
26 } 19 }
  20 +
  21 +func (e Error)Error()string{
  22 + if e.err==nil{
  23 + return ""
  24 + }
  25 + return e.err.Error()
  26 +}
  27 +
  28 +func(e Error)Unwrap()error{
  29 + return e.err
  30 +}
1 package common 1 package common
2 2
3 import ( 3 import (
  4 + "errors"
4 "fmt" 5 "fmt"
5 "testing" 6 "testing"
6 ) 7 )
@@ -13,3 +14,18 @@ func Test_Error(t *testing.T){ @@ -13,3 +14,18 @@ func Test_Error(t *testing.T){
13 emsg :=NewErrorWithMsg(2,"some error") 14 emsg :=NewErrorWithMsg(2,"some error")
14 t.Log(emsg,emsg.Code) 15 t.Log(emsg,emsg.Code)
15 } 16 }
  17 +
  18 +func Test_AssertError(t *testing.T){
  19 + var targetErr = NewError(1,fmt.Errorf("%v","some error"))
  20 + var e error =targetErr
  21 + if !errors.Is(e,targetErr){
  22 + t.Fatal("errors.Is not equal")
  23 + }
  24 + if errors.Unwrap(e) ==nil{
  25 + t.Fatal("errors.Unwrap not nil")
  26 + }
  27 + var commErr Error
  28 + if !errors.As(e,&commErr){
  29 + t.Fatal("errors.As error")
  30 + }
  31 +}
@@ -33,17 +33,16 @@ func (this *BaseController) Options() { @@ -33,17 +33,16 @@ func (this *BaseController) Options() {
33 func (this *BaseController) AllowCross() { 33 func (this *BaseController) AllowCross() {
34 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") 34 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
35 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") 35 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
36 - //this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "uid, token,jwt, deviceid, appid,Content-Type,Authorization,from")  
37 - this.Ctx.WriteString("") 36 + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
  37 + //this.Ctx.WriteString("")
38 } 38 }
39 39
40 func (this *BaseController) Prepare() { 40 func (this *BaseController) Prepare() {
41 -  
42 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") 41 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
  42 + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
43 if this.Ctx.Input.Method() == "OPTIONS" { 43 if this.Ctx.Input.Method() == "OPTIONS" {
44 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") 44 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
45 - //this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "uid, token,jwt, deviceid, appid,Content-Type,Authorization,from")  
46 - this.Ctx.WriteString("") 45 + //this.Ctx.WriteString("")
47 return 46 return
48 } 47 }
49 48
@@ -118,34 +117,3 @@ func (this *BaseController) Finish() { @@ -118,34 +117,3 @@ func (this *BaseController) Finish() {
118 log.Info(fmt.Sprintf("<====Send to uid(%d) client: %d byte\nRequestId:%s RspBodyData: %s", this.RequestHead.Uid, length,this.RequestHead.GetRequestId(), string(strByte))) 117 log.Info(fmt.Sprintf("<====Send to uid(%d) client: %d byte\nRequestId:%s RspBodyData: %s", this.RequestHead.Uid, length,this.RequestHead.GetRequestId(), string(strByte)))
119 } 118 }
120 } 119 }
121 -  
122 -// BaseControllerCallBack  
123 -type BaseControllerCallBack struct {  
124 - beego.Controller  
125 - Query map[string]string  
126 - JSONBody map[string]interface{}  
127 - ByteBody []byte  
128 -}  
129 -  
130 -func (this *BaseControllerCallBack) Prepare() {  
131 - this.Query = map[string]string{}  
132 - input := this.Input()  
133 - for k := range input {  
134 - this.Query[k] = input.Get(k)  
135 - }  
136 -  
137 - if this.Ctx.Input.RequestBody != nil {  
138 - log.Info("RecvHead:", string(this.Ctx.Input.Header("Authorization")))  
139 - this.ByteBody = this.Ctx.Input.RequestBody  
140 - }  
141 -}  
142 -  
143 -func (this *BaseControllerCallBack) Resp(msg *Message) {  
144 - this.Data["json"] = msg  
145 - this.ServeJSON()  
146 -}  
147 -  
148 -func (this *BaseControllerCallBack) Finish() {  
149 - strByte, _ := json.Marshal(this.Data["json"])  
150 - log.Debug("<====Send to client:\n", string(strByte))  
151 -}  
@@ -65,3 +65,7 @@ func init() { @@ -65,3 +65,7 @@ func init() {
65 ErrnoMsg[5] = "描述包含敏感词,请重新编辑" 65 ErrnoMsg[5] = "描述包含敏感词,请重新编辑"
66 ErrnoMsg[6] ="重复提交,请稍后再试" 66 ErrnoMsg[6] ="重复提交,请稍后再试"
67 } 67 }
  68 +
  69 +func SetMessage(code int,msg string){
  70 + ErrnoMsg[code] = msg
  71 +}