go-jarvis / rum-gonic

a gin based RESTful web framework, inspired by cobra
MIT License
0 stars 0 forks source link

support status error with code #5

Closed tangx closed 2 years ago

tangx commented 2 years ago

output error handle logic is ugly https://github.com/go-jarvis/rum-gonic/blob/master/rum/group.go#L150

try to use statuserrors to prove it

tangx commented 2 years ago

https://github.com/go-jarvis/rum-gonic/issues/2

// 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()
}