Skip to content

Commit

Permalink
feat(Errors): Errors use interface can inject string or struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
cutedogspark committed Mar 12, 2018
1 parent 5b20421 commit b9d6538
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 30 deletions.
18 changes: 4 additions & 14 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,10 @@ type errorCall struct {
responseParams ErrorResponse
}

type ErrorProto struct {
Domain string `json:"domain,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
Location string `json:"location,omitempty"`
LocationType string `json:"location_type,omitempty"`
ExtendedHelp string `json:"extended_help,omitempty"`
SendReport string `json:"send_report,omitempty"`
}

type errorMessage struct {
Code int `json:"code"`
Message string `json:"message"`
Errors []ErrorProto `json:"errors,omitempty"`
Code int `json:"code"`
Message string `json:"message"`
Errors []interface{} `json:"errors,omitempty"`
}

type ErrorResponse struct {
Expand All @@ -129,7 +119,7 @@ func (r *errorCall) Code(code int) *errorCall {
return r
}

func (r *errorCall) Errors(errors []ErrorProto) *errorCall {
func (r *errorCall) Errors(errors []interface{}) *errorCall {
r.responseParams.Error.Errors = errors
return r
}
Expand Down
57 changes: 41 additions & 16 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,62 @@ func TestCustomCtx(t *testing.T) {
wantJSON: `{"apiVersion": "1.0", "data": "hello world"}`,
},
{
name: "400",
name: "400 with google json style",
givenHandler: func(c echo.Context) error {

var errDate []ctx.ErrorProto

errCode := 40000001

errDate = append(errDate, ctx.ErrorProto{
Domain: "Calendar",
Reason: "ResourceNotFoundException",
Message: "Resources is not exist",
errData := ctx.NewErrorProto()

errData.Add(ctx.ErrorProtoItem{
Domain: "Calendar",
Reason: "ResourceNotFoundException",
Message: "Resources is not exist",
LocationType: "database query",
Location: "query",
Location: "query",
ExtendedHelp: "http://help-link",
SendReport: "http://report.dajui.com/",
SendReport: "http://report.dajui.com/",
})

errMsg := errDate[0].Message
errMsg := errData.Items[0].Message

errDate = append(errDate, ctx.ErrorProto{
Domain: "global",
Reason: "required",
Message: "Required parameter: part",
errData.Add(ctx.ErrorProtoItem{
Domain: "global",
Reason: "required",
Message: "Required parameter: part",
LocationType: "parameter",
Location: "part",
Location: "part",
})

return c.(ctx.CustomCtx).Resp(errCode).Error(fmt.Sprintf("%v", errMsg)).Code(errCode).Errors(errDate).Do()
return c.(ctx.CustomCtx).Resp(errCode).Error(fmt.Sprintf("%v", errMsg)).Code(errCode).Errors(errData.AsErrors()).Do()
},
wantJSON: `{"apiVersion":"1.0","error":{"code":40000001,"message":"Resources is not exist","errors":[{"extended_help":"http://help-link", "send_report":"http://report.dajui.com/", "domain":"Calendar", "reason":"ResourceNotFoundException", "message":"Resources is not exist", "location":"query", "location_type":"database query"},{"message":"Required parameter: part", "location":"part", "location_type":"parameter", "domain":"global", "reason":"required"}]}}`,
},
{
name: "400 with string errors",
givenHandler: func(c echo.Context) error {

errCode := 40000001
errMsg := "Error Title"
errDate := ctx.NewErrors()
errDate.Add("Error Message 1")
errDate.Add("Error Message 2")

return c.(ctx.CustomCtx).Resp(errCode).Error(fmt.Sprintf("%v", errMsg)).Code(errCode).Errors(errDate.Error()).Do()
},
wantJSON: `{"apiVersion":"1.0","error":{"code":40000001,"message":"Error Title","errors":["Error Message 1","Error Message 2"]}}`,
},
{
name: "400 with custom struct",
givenHandler: func(c echo.Context) error {
errs := []interface{}{}
errs = append(errs, struct {
Name string `json:"name"`
}{"peter"})
return c.(ctx.CustomCtx).Resp(http.StatusOK).Error("this is error message").Errors(errs).Do()
},
wantJSON: `{"apiVersion":"1.0","error":{"code":0, "message":"this is error message", "errors":[{"name":"peter"}]}}`,
},
}

for _, tc := range tt {
Expand Down
35 changes: 35 additions & 0 deletions proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ctx

type ErrorProto struct {
Items []ErrorProtoItem
}

type ErrorProtoItem struct {
Domain string `json:"domain,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
Location string `json:"location,omitempty"`
LocationType string `json:"location_type,omitempty"`
ExtendedHelp string `json:"extended_help,omitempty"`
SendReport string `json:"send_report,omitempty"`
}

func NewErrorProto() *ErrorProto { return &ErrorProto{} }

func NewErrorProtoItem() *ErrorProtoItem { return &ErrorProtoItem{} }

func (e *ErrorProto) Add(d ErrorProtoItem) *ErrorProto {

e.Items = append(e.Items, d)

return e
}

func (e *ErrorProto) AsErrors() []interface{} {

errs := make([]interface{}, len(e.Items))
for i, v := range e.Items {
errs[i] = v
}
return errs
}

0 comments on commit b9d6538

Please sign in to comment.