Browse Source

error: use application/json as the content-type

Fixes #1584
Brandon Philips 11 years ago
parent
commit
5fbef59dbc
2 changed files with 72 additions and 15 deletions
  1. 20 15
      error/error.go
  2. 52 0
      error/error_test.go

+ 20 - 15
error/error.go

@@ -63,6 +63,16 @@ var errors = map[int]string{
 	EcodeClientInternal: "Client Internal Error",
 }
 
+var errorStatus = map[int]int{
+	EcodeKeyNotFound:  http.StatusNotFound,
+	EcodeNotFile:      http.StatusForbidden,
+	EcodeDirNotEmpty:  http.StatusForbidden,
+	EcodeTestFailed:   http.StatusPreconditionFailed,
+	EcodeNodeExist:    http.StatusPreconditionFailed,
+	EcodeRaftInternal: http.StatusInternalServerError,
+	EcodeLeaderElect:  http.StatusInternalServerError,
+}
+
 const (
 	EcodeKeyNotFound      = 100
 	EcodeTestFailed       = 101
@@ -133,22 +143,17 @@ func (e Error) toJsonString() string {
 	return string(b)
 }
 
+func (e Error) statusCode() int {
+	status, ok := errorStatus[e.ErrorCode]
+	if !ok {
+		status = http.StatusBadRequest
+	}
+	return status
+}
+
 func (e Error) WriteTo(w http.ResponseWriter) {
 	w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
-	// 3xx is raft internal error
-	status := http.StatusBadRequest
-	switch e.ErrorCode {
-	case EcodeKeyNotFound:
-		status = http.StatusNotFound
-	case EcodeNotFile, EcodeDirNotEmpty:
-		status = http.StatusForbidden
-	case EcodeTestFailed, EcodeNodeExist:
-		status = http.StatusPreconditionFailed
-	default:
-		if e.ErrorCode/100 == 3 {
-			status = http.StatusInternalServerError
-		}
-	}
-	w.WriteHeader(status)
+	w.Header().Set("Content-Type", "application/json")
+	w.WriteHeader(e.statusCode())
 	fmt.Fprintln(w, e.toJsonString())
 }

+ 52 - 0
error/error_test.go

@@ -0,0 +1,52 @@
+/*
+   Copyright 2014 CoreOS, Inc.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+package error
+
+import (
+	"net/http"
+	"net/http/httptest"
+	"reflect"
+	"strings"
+	"testing"
+)
+
+func TestErrorWriteTo(t *testing.T) {
+	for k, _ := range errors {
+		err := NewError(k, "", 1)
+		rr := httptest.NewRecorder()
+		err.WriteTo(rr)
+
+		if err.statusCode() != rr.Code {
+			t.Errorf("HTTP status code %d, want %d", rr.Code, err.statusCode())
+		}
+
+		gbody := strings.TrimSuffix(rr.Body.String(), "\n")
+		if err.toJsonString() != gbody {
+			t.Errorf("HTTP body %q, want %q", gbody, err.toJsonString())
+		}
+
+		wheader := http.Header(map[string][]string{
+			"Content-Type": []string{"application/json"},
+			"X-Etcd-Index": []string{"1"},
+		})
+
+		if !reflect.DeepEqual(wheader, rr.HeaderMap) {
+			t.Errorf("HTTP headers %v, want %v", rr.HeaderMap, wheader)
+		}
+	}
+
+}