Skip to content

Commit

Permalink
Merge pull request #3 from cutedogspark/feature/error-structure
Browse files Browse the repository at this point in the history
feat(Errors): Errors structure follow google json style guides
  • Loading branch information
cutedogspark authored Mar 12, 2018
2 parents 0a0bbb3 + b9d6538 commit d694b0a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 140 deletions.
36 changes: 34 additions & 2 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,39 @@ 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 {

errCode := 40000001

errData := ctx.NewErrorProto()

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

errMsg := errData.Items[0].Message

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

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
Expand All @@ -40,7 +72,7 @@ func TestCustomCtx(t *testing.T) {
wantJSON: `{"apiVersion":"1.0","error":{"code":40000001,"message":"Error Title","errors":["Error Message 1","Error Message 2"]}}`,
},
{
name: "400 with error and errors",
name: "400 with custom struct",
givenHandler: func(c echo.Context) error {
errs := []interface{}{}
errs = append(errs, struct {
Expand Down
63 changes: 0 additions & 63 deletions example/Gopkg.lock

This file was deleted.

30 changes: 0 additions & 30 deletions example/Gopkg.toml

This file was deleted.

45 changes: 0 additions & 45 deletions example/server.go

This file was deleted.

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 d694b0a

Please sign in to comment.