Skip to content

Commit

Permalink
devlxd: Unmask Render errors (#14760)
Browse files Browse the repository at this point in the history
This implements a better handling of errors during Render on devlxd
handlers, following the same pattern seen on
https://github.com/hamistao/lxd/blob/unmask_write_errors/lxd/daemon.go#L846
When rendering a response (send it to the client) fails, instead of
masking the error we try to send a response using `DevLxdErrorResponse`
to the client communicating the error, since this determines the
response type used for communicating erros on the devlxd API. If that
also fails, an error is logged on the server.
  • Loading branch information
tomponline authored Jan 9, 2025
2 parents 78f4a86 + c68d515 commit 5235a07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lxd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,15 @@ func hoistReqVM(f func(*Daemon, instance.Instance, http.ResponseWriter, *http.Re
}

resp := f(d, inst, w, r)
_ = resp.Render(w, r)
if resp != nil {
err = resp.Render(w, r)
if err != nil {
writeErr := response.DevLxdErrorResponse(err, true).Render(w, r)
if writeErr != nil {
logger.Warn("Failed writing error for HTTP response", logger.Ctx{"url": r.URL, "err": err, "writeErr": writeErr})
}
}
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion lxd/devlxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,15 @@ func hoistReq(f func(*Daemon, instance.Instance, http.ResponseWriter, *http.Requ
}

resp := f(d, c, w, r)
_ = resp.Render(w, r)
if resp != nil {
err = resp.Render(w, r)
if err != nil {
writeErr := response.DevLxdErrorResponse(err, false).Render(w, r)
if writeErr != nil {
logger.Warn("Failed writing error for HTTP response", logger.Ctx{"url": r.URL, "err": err, "writeErr": writeErr})
}
}
}
}
}

Expand Down

0 comments on commit 5235a07

Please sign in to comment.