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(http): return error in case of panic #250

Merged
merged 1 commit into from
Apr 15, 2024
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
12 changes: 12 additions & 0 deletions http/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ func TestErrors(t *testing.T) {
status: "200 OK",
bodyStr: "the reader call returns a reader.",
},

{
path: []string{"panic"},
status: "500 Internal Server Error",
bodyStr: `{"Message":"an error occurred","Code":0,"Type":"error"}` + "\n",
},
{
path: []string{"latepanic"},
status: "200 OK",
bodyStr: `"some value"` + "\n",
errTrailer: "an error occurred",
},
}

mkTest := func(tc testcase) func(*testing.T) {
Expand Down
12 changes: 11 additions & 1 deletion http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,21 @@
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Debug("incoming API request: ", r.URL)

var re ResponseEmitter

defer func() {
if r := recover(); r != nil {
log.Error("a panic has occurred in the commands handler!")
log.Error(r)
log.Errorf("stack trace:\n%s", debug.Stack())

if re != nil {
if err := re.CloseWithError(errors.New("an error occurred")); err != nil {
log.Errorf("error closing ResponseEmitter: %s", err)

Check warning on line 102 in http/handler.go

View check run for this annotation

Codecov / codecov/patch

http/handler.go#L102

Added line #L102 was not covered by tests

return

Check warning on line 104 in http/handler.go

View check run for this annotation

Codecov / codecov/patch

http/handler.go#L104

Added line #L104 was not covered by tests
}
}
}
}()

Expand Down Expand Up @@ -178,7 +188,7 @@
}
defer cancel()

re, err := NewResponseEmitter(w, r.Method, req, withRequestBodyEOFChan(bodyEOFChan))
re, err = NewResponseEmitter(w, r.Method, req, withRequestBodyEOFChan(bodyEOFChan))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
13 changes: 13 additions & 0 deletions http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ var (
}),
},
},

"panic": {
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
panic("Invalid memory address or nil pointer dereference")
},
},
"latepanic": {
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
re.Emit("some value")
panic("Invalid memory address or nil pointer dereference")
},
Type: "",
},
},
}
)
Expand Down