http: respondCommon to do common responses

This commit is contained in:
Mitchell Hashimoto
2015-03-31 21:29:53 -07:00
parent fd1d9b1631
commit 8c707df4bc
3 changed files with 16 additions and 8 deletions

View File

@@ -57,9 +57,18 @@ func respondError(w http.ResponseWriter, status int, err error) {
enc.Encode(resp) enc.Encode(resp)
} }
func respondErrorResponse(w http.ResponseWriter, resp *logical.Response) { func respondCommon(w http.ResponseWriter, resp *logical.Response) bool {
err := fmt.Errorf("%s", resp.Data["error"].(string)) if resp == nil {
respondError(w, http.StatusBadRequest, err) return false
}
if resp.IsError() {
err := fmt.Errorf("%s", resp.Data["error"].(string))
respondError(w, http.StatusBadRequest, err)
return true
}
return false
} }
func respondOk(w http.ResponseWriter, body interface{}) { func respondOk(w http.ResponseWriter, body interface{}) {

View File

@@ -59,12 +59,11 @@ func handleLogical(core *vault.Core) http.Handler {
respondError(w, http.StatusInternalServerError, err) respondError(w, http.StatusInternalServerError, err)
return return
} }
if op == logical.ReadOperation && resp == nil { if respondCommon(w, resp) {
respondError(w, http.StatusNotFound, nil)
return return
} }
if resp.IsError() { if op == logical.ReadOperation && resp == nil {
respondErrorResponse(w, resp) respondError(w, http.StatusNotFound, nil)
return return
} }

View File

@@ -26,7 +26,6 @@ func TestLogical(t *testing.T) {
var actual map[string]interface{} var actual map[string]interface{}
expected := map[string]interface{}{ expected := map[string]interface{}{
"vault_id": "",
"renewable": false, "renewable": false,
"lease_duration": float64(0), "lease_duration": float64(0),
"data": map[string]interface{}{ "data": map[string]interface{}{
@@ -35,6 +34,7 @@ func TestLogical(t *testing.T) {
} }
testResponseStatus(t, resp, 200) testResponseStatus(t, resp, 200)
testResponseBody(t, resp, &actual) testResponseBody(t, resp, &actual)
delete(actual, "vault_id")
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual) t.Fatalf("bad: %#v", actual)
} }