From 7a385fed7f80d2ab42844f6c7ca26dfd6c0e68a5 Mon Sep 17 00:00:00 2001 From: Florian Reiterer Date: Tue, 26 Jan 2016 09:36:15 +0100 Subject: [PATCH] Better grpc error strings Closes #87 --- runtime/errors.go | 3 ++- runtime/errors_test.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/runtime/errors.go b/runtime/errors.go index 3525e2c6a01..98c1950d4de 100644 --- a/runtime/errors.go +++ b/runtime/errors.go @@ -64,6 +64,7 @@ var ( type errorBody struct { Error string `json:"error"` + Code int `json:"code"` } // DefaultHTTPError is the default implementation of HTTPError. @@ -76,7 +77,7 @@ func DefaultHTTPError(ctx context.Context, w http.ResponseWriter, _ *http.Reques const fallback = `{"error": "failed to marshal error message"}` w.Header().Set("Content-Type", "application/json") - body := errorBody{Error: err.Error()} + body := errorBody{Error: grpc.ErrorDesc(err), Code: int(grpc.Code(err))} buf, merr := json.Marshal(body) if merr != nil { glog.Errorf("Failed to marshal error message %q: %v", body, merr) diff --git a/runtime/errors_test.go b/runtime/errors_test.go index 9471c1d76a1..9f5022aa3c9 100644 --- a/runtime/errors_test.go +++ b/runtime/errors_test.go @@ -44,12 +44,12 @@ func TestDefaultHTTPError(t *testing.T) { t.Errorf("w.Code = %d; want %d", got, want) } - body := make(map[string]string) + body := make(map[string]interface{}) if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { t.Errorf("json.Unmarshal(%q, &body) failed with %v; want success", w.Body.Bytes(), err) continue } - if got, want := body["error"], spec.msg; !strings.Contains(got, want) { + if got, want := body["error"].(string), spec.msg; !strings.Contains(got, want) { t.Errorf(`body["error"] = %q; want %q; on spec.err=%v`, got, want, spec.err) } }