Skip to content

Commit

Permalink
Merge pull request #94 from floridoo/master
Browse files Browse the repository at this point in the history
Better grpc error strings
  • Loading branch information
yugui committed Jan 27, 2016
2 parents dd0938e + 7a385fe commit 820fef9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion runtime/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var (

type errorBody struct {
Error string `json:"error"`
Code int `json:"code"`
}

// DefaultHTTPError is the default implementation of HTTPError.
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions runtime/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 820fef9

Please sign in to comment.