Why not return after calling http.Error()

170 views
Skip to first unread message

Gurunandan Bhat

unread,
Jul 4, 2023, 11:39:05 AM7/4/23
to golan...@googlegroups.com

Every example of http Handler that I have seen so far looks like this:

func handleSomeRequest(w http.ResponseWriter, r *http.Request) {

    // do something that returns an error
    if err != nil {
        http.Error(w, "Something bad happened", http.SomeAppropriateStatus)
    }
    
    // do other things if there was no error above and finally
    w.Write(someBytes)
}

My question is: Assuming there is no point carrying on after "Something bad happened" why don't we just return (after writing someBytes to w inside the if error != nil if that is what is expected). I have never seen any code that returns after calling Error(), and I wonder why.


Thanks for the help.


Jan Mercl

unread,
Jul 4, 2023, 11:44:31 AM7/4/23
to Gurunandan Bhat, golan...@googlegroups.com
From the docs at https://pkg.go.dev/net/http#Error

""""
It does not otherwise end the request; the caller should ensure no
further writes are done to w.
""""

AFAICT you're right, correct handler code should return after calling
http.Error.

ben...@gmail.com

unread,
Jul 4, 2023, 6:43:35 PM7/4/23
to golang-nuts
> Every example of http Handler that I have seen so far looks like this:

Yes, that's definitely odd. I just grepped my Go source directory (which includes the Go compiler and stdlib and a bunch of other Go code), and almost all of them have a "return" on the next line. For example:

$ rg --type=go --sort=path 'http\.Error\(' -A1
...
pkgsite/internal/worker/server.go
319: http.Error(w, http.StatusText(code), code)
320- return

-Ben

Volker Dobler

unread,
Jul 5, 2023, 1:52:53 AM7/5/23
to golang-nuts
On Tuesday, 4 July 2023 at 17:39:05 UTC+2 Gurunandan Bhat wrote:

Every example of http Handler that I have seen so far looks like this:

This is very strange as basically all correct http Handlers I have
seen in the last 10 years actually do return after http.Error.

If all examples of http Handler you have seen continue (and
even send on w) after http.Error you should reconsider the
source(s) of your examples. This seems more to be a question
about the quality of the examples you consult than on what to
do after http.Error.

V.
 

Gurunandan Bhat

unread,
Jul 6, 2023, 3:28:04 AM7/6/23
to golang-nuts
Thanks to everyone who reassured me. 
I considered posting a set of links from the most popular Golang tutorials that do not return after calling http.Error(), but then baulked 

In any case, thanks once again to all in this thread.

Regards


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7a727dd2-e39a-4414-b323-6f3ff2a27aa7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages