Doing a little better with http response codes

This commit is contained in:
Nate Brown
2015-06-19 13:56:44 -07:00
parent ee176b2f5d
commit ed0853ce18
2 changed files with 20 additions and 5 deletions

View File

@@ -80,7 +80,7 @@ func request(core *vault.Core, w http.ResponseWriter, rawReq *http.Request, r *l
respondStandby(core, w, rawReq.URL)
return resp, false
}
if respondCommon(w, resp) {
if respondCommon(w, resp, err) {
return resp, false
}
if err != nil {
@@ -168,14 +168,29 @@ func respondError(w http.ResponseWriter, status int, err error) {
enc.Encode(resp)
}
func respondCommon(w http.ResponseWriter, resp *logical.Response) bool {
func respondCommon(w http.ResponseWriter, resp *logical.Response, err error) bool {
if resp == nil {
return false
}
if resp.IsError() {
var statusCode int
switch err {
case logical.ErrPermissionDenied:
statusCode = http.StatusForbidden
case logical.ErrUnsupportedOperation:
statusCode = http.StatusMethodNotAllowed
case logical.ErrUnsupportedPath:
statusCode = http.StatusNotFound
case logical.ErrInvalidRequest:
statusCode = http.StatusBadRequest
default:
statusCode = http.StatusBadRequest
}
err := fmt.Errorf("%s", resp.Data["error"].(string))
respondError(w, http.StatusBadRequest, err)
respondError(w, statusCode, err)
return true
}