I'm trying to rewrite a small node-api in golang and there I found that the internal error doesn't stop the processing of the requests. Is there a nice way to end the processing?
I've tried different things like making a error-object and using panic(err) to exit the processing but I think there are better ways.
I expected a function which stops the processing of the request and throws an 500-error and maybe logs a thing or two.
The code-sample is like this:
package main
import (
"fmt"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/ping/", ping)
err := http.ListenAndServe(":8080", nil)
checkErr(err)
}
func ping(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("pong
"))
urlPath := req.URL.Path
rest := strings.TrimPrefix(urlPath, "/ping/")
testRest(w, rest)
}
func testRest(w http.ResponseWriter, rest string) {
if rest == "abc" {
http.Error(w, "The go-routine should stop here", 500)
}
fmt.Print("This shouldn't be printed")
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
And I call it like that:
curl --url http://127.0.0.1:8080/ping/abc