Merge pull request #12687 from WeiZhang555/NotFound

refactor httpError() and add 404 "not found" mapping
This commit is contained in:
Jessie Frazelle 2015-04-23 11:21:09 -07:00
commit 2345389975

View file

@ -203,18 +203,19 @@ func httpError(w http.ResponseWriter, err error) {
// If we need to differentiate between different possible error types, we should // If we need to differentiate between different possible error types, we should
// create appropriate error types with clearly defined meaning. // create appropriate error types with clearly defined meaning.
errStr := strings.ToLower(err.Error()) errStr := strings.ToLower(err.Error())
if strings.Contains(errStr, "no such") { for keyword, status := range map[string]int{
statusCode = http.StatusNotFound "not found": http.StatusNotFound,
} else if strings.Contains(errStr, "bad parameter") { "no such": http.StatusNotFound,
statusCode = http.StatusBadRequest "bad parameter": http.StatusBadRequest,
} else if strings.Contains(errStr, "conflict") { "conflict": http.StatusConflict,
statusCode = http.StatusConflict "impossible": http.StatusNotAcceptable,
} else if strings.Contains(errStr, "impossible") { "wrong login/password": http.StatusUnauthorized,
statusCode = http.StatusNotAcceptable "hasn't been activated": http.StatusForbidden,
} else if strings.Contains(errStr, "wrong login/password") { } {
statusCode = http.StatusUnauthorized if strings.Contains(errStr, keyword) {
} else if strings.Contains(errStr, "hasn't been activated") { statusCode = status
statusCode = http.StatusForbidden break
}
} }
logrus.WithFields(logrus.Fields{"statusCode": statusCode, "err": err}).Error("HTTP Error") logrus.WithFields(logrus.Fields{"statusCode": statusCode, "err": err}).Error("HTTP Error")