Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better grpc error strings #94

Merged
merged 1 commit into from
Jan 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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