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

fix: use golang's http.Error to send errors #167

Merged
merged 2 commits into from
May 25, 2019
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
6 changes: 0 additions & 6 deletions CODEOWNERS

This file was deleted.

2 changes: 1 addition & 1 deletion http/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestErrors(t *testing.T) {
cmds.EncLong: "foobar",
},
status: "400 Bad Request",
bodyStr: `invalid encoding: foobar`,
bodyStr: "invalid encoding: foobar\n",
},

{
Expand Down
14 changes: 6 additions & 8 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}()

if !allowOrigin(r, h.cfg) || !allowReferer(r, h.cfg) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("403 - Forbidden"))
http.Error(w, "403 - Forbidden", http.StatusForbidden)
log.Warningf("API blocked request to %s. (possible CSRF)", r.URL)
return
}
Expand All @@ -122,12 +121,12 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

req, err := parseRequest(r, h.root)
if err != nil {
status := http.StatusBadRequest
if err == ErrNotFound {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusBadRequest)
status = http.StatusNotFound
}
w.Write([]byte(err.Error()))

http.Error(w, err.Error(), status)
return
}

Expand All @@ -146,8 +145,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

re, err := NewResponseEmitter(w, r.Method, req, withRequestBodyEOFChan(bodyEOFChan))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

Expand Down