From 404b1fcfadb2730ea29c062ac2c932d63169b108 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 16 Apr 2018 18:08:58 +0800 Subject: [PATCH] feat(gctx): gctx support append domain --- ctx_test.go | 34 ++++++++++++++++++++--- example/GError/main.go | 61 ++++++++++++++++++++++++++++++++++++++++++ gctx.go | 4 +-- gerr.go | 15 ++++++++--- 4 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 example/GError/main.go diff --git a/ctx_test.go b/ctx_test.go index 74230be..350f41c 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -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", @@ -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", @@ -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"}]}}`, }, @@ -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 { diff --git a/example/GError/main.go b/example/GError/main.go new file mode 100644 index 0000000..45bb1c8 --- /dev/null +++ b/example/GError/main.go @@ -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")) + +} diff --git a/gctx.go b/gctx.go index dc773d3..16553c9 100644 --- a/gctx.go +++ b/gctx.go @@ -80,7 +80,7 @@ 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 { @@ -88,7 +88,7 @@ type GErrorResponse struct { 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{ diff --git a/gerr.go b/gerr.go index 37db11f..919bef1 100644 --- a/gerr.go +++ b/gerr.go @@ -1,6 +1,6 @@ package ctx -type GErrors []GError +type GErrors []*GError type GError struct { Code uint `json:"-"` @@ -13,8 +13,15 @@ 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 } @@ -22,4 +29,4 @@ func (c GErrors) Empty() bool { return len(c) == 0 } -func NewGErrors() GErrors { return GErrors{} } +func NewGErrors() *GErrors { return &GErrors{} }