Skip to content

Commit

Permalink
feat(gctx): gctx support append domain
Browse files Browse the repository at this point in the history
  • Loading branch information
cutedogspark committed Apr 16, 2018
1 parent b9ca0e0 commit 404b1fc
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
34 changes: 31 additions & 3 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestCustomCtx(t *testing.T) {
name: "400 with google json style",
givenHandler: func(c echo.Context) error {

gerrs := ctx.NewGErrors().Append(ctx.GError{
gerrs := ctx.NewGErrors().Append(&ctx.GError{
Code: 40000001,
Domain: "Calendar",
Reason: "ResourceNotFoundException",
Expand All @@ -45,7 +45,7 @@ func TestCustomCtx(t *testing.T) {
Location: "query",
ExtendedHelp: "http://help-link",
SendReport: "http://report.dajui.com/",
}).Append(ctx.GError{
}).Append(&ctx.GError{
Code: 40000001,
Domain: "global",
Reason: "required",
Expand All @@ -54,7 +54,7 @@ func TestCustomCtx(t *testing.T) {
Location: "part",
})

return c.(ctx.CustomCtx).GResp().Errors(gerrs...).Do()
return c.(ctx.CustomCtx).GResp().Errors(*gerrs...).Do()
},
wantJSON: `{"apiVersion":"1.0","error":{"code":40000001,"message":"Resources is not exist","errors":[{"extendedHelp":"http://help-link", "sendReport":"http://report.dajui.com/", "domain":"Calendar", "reason":"ResourceNotFoundException", "message":"Resources is not exist", "location":"query", "locationType":"database query"},{"message":"Required parameter: part", "location":"part", "locationType":"parameter", "domain":"global", "reason":"required"}]}}`,
},
Expand Down Expand Up @@ -83,6 +83,34 @@ func TestCustomCtx(t *testing.T) {
},
wantJSON: `{"apiVersion":"1.0","error":{"code":0, "message":"this is error message", "errors":[{"name":"peter"}]}}`,
},
{
name: "append domain ",
givenHandler: func(c echo.Context) error {

gerrs := ctx.NewGErrors().Append(&ctx.GError{
Code: 40000001,
Domain: "Calendar",
Reason: "ResourceNotFoundException",
Message: "Resources is not exist",
LocationType: "database query",
Location: "query",
ExtendedHelp: "http://help-link",
SendReport: "http://report.dajui.com/",
}).Append(&ctx.GError{
Code: 40000001,
Domain: "global",
Reason: "required",
Message: "Required parameter: part",
LocationType: "parameter",
Location: "part",
})

gerrs.AppendDomain("handler")

return c.(ctx.CustomCtx).GResp().Errors(*gerrs...).Do()
},
wantJSON: `{"apiVersion":"1.0","error":{"code":40000001,"message":"Resources is not exist","errors":[{"extendedHelp":"http://help-link", "sendReport":"http://report.dajui.com/", "domain":"handler.Calendar", "reason":"ResourceNotFoundException", "message":"Resources is not exist", "location":"query", "locationType":"database query"},{"message":"Required parameter: part", "location":"part", "locationType":"parameter", "domain":"handler.global", "reason":"required"}]}}`,
},
}

for _, tc := range tt {
Expand Down
61 changes: 61 additions & 0 deletions example/GError/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"github.com/cutedogspark/echo-custom-context"
"github.com/labstack/echo"
"net/http"
)

func main() {

// Echo instance
e := echo.New()
e.HideBanner = true
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return next(ctx.CustomCtx{c})
}
})

e.GET("/", func(c echo.Context) error {
return c.(ctx.CustomCtx).GResp(http.StatusOK).Data("Service").Do()
})

e.GET("/error", func(c echo.Context) error {
return c.(ctx.CustomCtx).GResp().Errors(&ctx.GError{
Code: 40001002,
Reason: "ParameterInvalid",
Domain: "error",
Message: "parameter required : id",
Location: "id",
LocationType: "parameter",
}).Do()
})

e.GET("/errors", func(c echo.Context) error {

ctxErr := ctx.NewGErrors().Append(&ctx.GError{
Code: 40001003,
Reason: "ParameterInvalid",
Domain: "validate",
Message: "parameter required : id",
Location: "id",
LocationType: "parameter",
}).Append(&ctx.GError{
Code: 40001004,
Reason: "RecordNotFound",
Domain: "repository",
Message: "record not found : id",
Location: "id",
LocationType: "user",
})

ctxErr.AppendDomain("handler")

return c.(ctx.CustomCtx).GResp().Errors(*ctxErr...).Do()
})

// Start server
e.Logger.Fatal(e.Start(":1323"))

}
4 changes: 2 additions & 2 deletions gctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ type gerrorCall struct {
type gerrorMessage struct {
Code uint `json:"code"`
Message string `json:"message"`
Errors []GError `json:"errors,omitempty"`
Errors []*GError `json:"errors,omitempty"`
}

type GErrorResponse struct {
ApiVersion string `json:"apiVersion"`
Error gerrorMessage `json:"error"`
}

func (r *grespCall) Errors(errs ...GError) *gerrorCall {
func (r *grespCall) Errors(errs ...*GError) *gerrorCall {
rs := &gerrorCall{
c: r.c,
responseParams: GErrorResponse{
Expand Down
15 changes: 11 additions & 4 deletions gerr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ctx

type GErrors []GError
type GErrors []*GError

type GError struct {
Code uint `json:"-"`
Expand All @@ -13,13 +13,20 @@ type GError struct {
SendReport string `json:"sendReport,omitempty"`
}

func (c GErrors) Append(gerr GError) GErrors {
c = append(c, gerr)
func (c *GErrors) Append(gErr *GError) *GErrors {
*c = append(*c, gErr)
return c
}

func (c *GErrors) AppendDomain(domain string) *GErrors {
for idx := range *c {
(*c)[idx].Domain = domain + "." + (*c)[idx].Domain
}
return c
}

func (c GErrors) Empty() bool {
return len(c) == 0
}

func NewGErrors() GErrors { return GErrors{} }
func NewGErrors() *GErrors { return &GErrors{} }

0 comments on commit 404b1fc

Please sign in to comment.