Skip to content

Commit

Permalink
fix: handle statuserrors for message in response
Browse files Browse the repository at this point in the history
  • Loading branch information
tangx committed Dec 10, 2021
1 parent 30cf8a0 commit 5e7387e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/gin-gonic/gin v1.7.7
github.com/go-jarvis/statuserrors v0.1.0
github.com/tangx/ginbinder v0.1.2
)

Expand All @@ -26,3 +27,5 @@ require (
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/go-jarvis/statuserrors => /data/gopath/src/github.com/go-jarvis/statuserrors
12 changes: 12 additions & 0 deletions internal/example/apis/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package index

import (
"context"
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-jarvis/rum-gonic/internal/example/injector/redis"
"github.com/go-jarvis/rum-gonic/pkg/httpx"
"github.com/go-jarvis/statuserrors"
)

type Index struct {
Expand All @@ -15,6 +17,16 @@ type Index struct {
}

func (index *Index) Output(c *gin.Context) (interface{}, error) {
msg := c.Query("e")
if msg == "nil" {
err := statuserrors.New(http.StatusBadRequest, "invalid request")
return nil, err
}

if msg == "data" {
err := statuserrors.New(http.StatusBadRequest, "invalid request")
return "this is user define output data, not from error", err
}

return logic(c, index), nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/example/main.http
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GET http://127.0.0.1:8080/v0/ping/wangwu?age=102


### user
GET http://127.0.0.1:8080/index
GET http://127.0.0.1:8080/index?e=data123



Expand Down
57 changes: 50 additions & 7 deletions rum/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/go-jarvis/rum-gonic/pkg/httpx"
"github.com/go-jarvis/statuserrors"
"github.com/tangx/ginbinder"
)

Expand Down Expand Up @@ -131,7 +132,7 @@ func (r *RouterGroup) handlerfunc(op Operator) HandlerFunc {

err := ginbinder.ShouldBindRequest(c, op)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
r.output(c, nil, err)
return
}

Expand All @@ -147,15 +148,57 @@ func (r *RouterGroup) handlerfunc(op Operator) HandlerFunc {
return
}

if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}

c.JSON(http.StatusOK, ret)
r.output(c, ret, err)
}
}

func (r *RouterGroup) addOperators(ops ...Operator) {
r.operators = append(r.operators, ops...)
}

// output give response code and data
// content-type is text/plain if data is string type, or content-type
// is application/json by default. maybe it will support more
// content types in feture.
func (r *RouterGroup) output(c *gin.Context, data interface{}, err error) {
code, data := extract(data, err)

switch ret := data.(type) {
case string:
c.String(code, ret)
default:
c.JSON(code, ret)
}

}

// extract return http status code and output message,
// no matter if error is nil,
// if data is not nil, using data for output message, otherwise trying to use
// error Error() message as output message
func extract(data interface{}, err error) (code int, result interface{}) {

code, result = extractError(err)

if data != nil {
return code, data
}

return code, result
}

// extractError return http status code and error message
// if err is not a status error, try to return statuserrors status code
// and error message.
func extractError(err error) (code int, msg string) {
if err == nil {
return http.StatusOK, ""
}

if e, ok := err.(statuserrors.StatusError); ok {
return e.StatusCode(), err.Error()
}

sterr := statuserrors.New(statuserrors.StatusUnknownError, err.Error())
return statuserrors.StatusUnknownError, sterr.Error()
}

0 comments on commit 5e7387e

Please sign in to comment.