diff --git a/ctx.go b/ctx.go index 4e47103..fb216aa 100644 --- a/ctx.go +++ b/ctx.go @@ -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 { @@ -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 } diff --git a/ctx_test.go b/ctx_test.go index 8f947e0..8ae1e60 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -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 { diff --git a/proto.go b/proto.go new file mode 100644 index 0000000..163e91e --- /dev/null +++ b/proto.go @@ -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 +}