-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validate): support validate struct error and handler return GError
- Loading branch information
1 parent
0afcbaf
commit 6e59660
Showing
4 changed files
with
128 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,52 @@ | ||
package ctx | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/labstack/echo" | ||
"github.com/pkg/errors" | ||
"gopkg.in/go-playground/validator.v9" | ||
) | ||
|
||
func HTTPErrorHandler(err error, c echo.Context) { | ||
|
||
r := CustomCtx{c} | ||
code := http.StatusInternalServerError | ||
var msg interface{} | ||
if he, ok := err.(*echo.HTTPError); ok { | ||
code = he.Code | ||
msg = he.Message | ||
} | ||
if err := r.Resp(code).Error(fmt.Sprintf("%v", msg)).Code(code).Do(); err != nil { | ||
c.Logger().Error(err) | ||
} | ||
|
||
/* | ||
errDate := sri.NewErrors() | ||
errDate.Add(msg) | ||
if err := r.Resp(code).Error(fmt.Sprintf("%v", msg)).Code(code).Errors(errDate.Error()).Do(); err != nil { | ||
c.Logger().Error(err) | ||
if he, ok := err.(*GErrCall); ok { | ||
err = errors.WithStack(he) | ||
b, _ := json.Marshal(he.ResponseParams) | ||
c.JSONBlob(he.HttpStatus, b) | ||
} else if he, ok := err.(*GError); ok { | ||
err = errors.WithStack(he) | ||
gErrs := CustomCtx{}.GResp().Errors(he) | ||
b, _ := json.Marshal(gErrs.ResponseParams) | ||
c.JSONBlob(gErrs.HttpStatus, b) | ||
} else if he, ok := err.(*echo.HTTPError); ok { | ||
// warp echo error struct | ||
err = errors.WithStack(he) | ||
gErrs := CustomCtx{}.GResp().Errors(&GError{ | ||
Code: uint(he.Code), | ||
Message: fmt.Sprintf("%+v", he.Message), | ||
}) | ||
b, _ := json.Marshal(gErrs.ResponseParams) | ||
c.JSONBlob(gErrs.HttpStatus, b) | ||
} else if _, ok := err.(*validator.InvalidValidationError); !ok { | ||
var errMsg []string | ||
for _, err := range err.(validator.ValidationErrors) { | ||
errMsg = append(errMsg, fmt.Sprintf("%s:%s", err.Field(), err.ActualTag())) | ||
} | ||
{ | ||
"apiVersion": "v1", | ||
"error": { | ||
"code": 404, | ||
"message": "Not Found", | ||
"errors": [ | ||
"Not Found" | ||
] | ||
} | ||
} | ||
*/ | ||
gErrs := CustomCtx{}.GResp(http.StatusBadRequest).Errors(&GError{Code: http.StatusBadRequest, Message: strings.Join(errMsg, ",")}) | ||
b, _ := json.Marshal(gErrs.ResponseParams) | ||
c.JSONBlob(gErrs.HttpStatus, b) | ||
} else { | ||
// define unknown error message | ||
err = errors.New("unknown error") | ||
gErrs := CustomCtx{}.GResp().Errors(&GError{ | ||
Code: http.StatusInternalServerError, | ||
Message: err.Error(), | ||
}) | ||
b, _ := json.Marshal(gErrs.ResponseParams) | ||
c.JSONBlob(gErrs.HttpStatus, b) | ||
} | ||
c.Logger().Error(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters