Skip to content

Commit

Permalink
feat(Error): add Google JSON style error response (#4)
Browse files Browse the repository at this point in the history
implement error response with Google JSON style format
  • Loading branch information
cage1016 authored and cutedogspark committed Apr 11, 2018
1 parent d694b0a commit ab83acc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 47 deletions.
47 changes: 47 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ctx

import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/labstack/echo"
Expand Down Expand Up @@ -131,3 +133,48 @@ func (r *errorCall) Do() (err error) {
}
return r.c.JSONBlob(r.httpStatus, b)
}

// Google JSON Style error call
type gerrorCall struct {
c echo.Context
httpStatus int
responseParams GErrorResponse
}

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

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

func (c CustomCtx) GError(errs ...GError) *gerrorCall {
rs := &gerrorCall{
c: echo.Context(c),
responseParams: GErrorResponse{
ApiVersion: apiVersion,
Error: gerrorMessage{},
},
}

if len(errs) > 0 {
s, _ := strconv.Atoi(fmt.Sprintf("%d", errs[0].Code)[:3])
rs.httpStatus = s
rs.responseParams.Error.Code = errs[0].Code
rs.responseParams.Error.Message = errs[0].Message
rs.responseParams.Error.Errors = errs
}
return rs
}

func (r *gerrorCall) Do() (err error) {
b, err := json.Marshal(r.responseParams)
if err != nil {
return err
}
return r.c.JSONBlob(r.httpStatus, b)
}
18 changes: 6 additions & 12 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,27 @@ func TestCustomCtx(t *testing.T) {
name: "400 with google json style",
givenHandler: func(c echo.Context) error {

errCode := 40000001

errData := ctx.NewErrorProto()

errData.Add(ctx.ErrorProtoItem{
gerr := 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/",
})

errMsg := errData.Items[0].Message

errData.Add(ctx.ErrorProtoItem{
}).Append(ctx.GError{
Code: 40000001,
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()
return c.(ctx.CustomCtx).GError(gerr...).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"}]}}`,
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"}]}}`,
},
{
name: "400 with string errors",
Expand Down
21 changes: 21 additions & 0 deletions gerr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ctx

type GErrors []GError

type GError struct {
Code int `json:"-"`
Domain string `json:"domain,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
Location string `json:"location,omitempty"`
LocationType string `json:"locationType,omitempty"`
ExtendedHelp string `json:"extendedHelp,omitempty"`
SendReport string `json:"sendReport,omitempty"`
}

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

func NewGErrors() GErrors { return GErrors{} }
35 changes: 0 additions & 35 deletions proto.go

This file was deleted.

0 comments on commit ab83acc

Please sign in to comment.