A handler writes an error response and then carries on as if the request had succeeded
What it finds
``go if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } json.NewEncoder(w).Encode(result) ` http.Error writes the response, but the handler goes on: it writes more to the same response, which the client receives glued to the error, and does the work that the error should have stopped. A return after http.Error` was meant.
The call must be http.Error of net/http, resolved through the imports, as the last statement of the branch of an if without else, and the if must be followed by another statement in its block other than a return.
From the rule's source, http_error_without_return.rs.
package p import ( "encoding/json" "net/http" "strconv") func handle(w http.ResponseWriter, r *http.Request) { n, err := strconv.Atoi(r.URL.Query().Get("n")) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest)tx0 reports: the handler goes on after http.Error } _ = json.NewEncoder(w).Encode(n)} func fine(w http.ResponseWriter, r *http.Request) { n, err := strconv.Atoi(r.URL.Query().Get("n")) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } _ = json.NewEncoder(w).Encode(n)} func last(w http.ResponseWriter, err error) { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:http-error-without-return" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/go:http-error-without-return] reason="..."tx0 explain tx0/go:http-error-without-return shows the same in the terminal. Found a false positive? Report it.