Hey! I saw your post and to me it looks like the issue is in your JavaScript code. You're using response.statusText, which is *not* the response body. It's expected to see exactly what you're observing, because statusText just gives you the standard reason phrase for the status code (like "Forbidden" for 403), and not the actual text you set in Go with http.Error.
If you curl that endpoint, you should see that the Go code behaves correctly and returns your custom message in the *body*. So the Go server is likely doing the right thing — it's just that the JS code isn't reading the body where your "Whatever! I return an error" message lives. Try replacing response.statusText with await response.text() and you should get your custom message back. If that's not the case and your code snippet is just not representing what you use, share the corrected version in a follow up. Hope that helps. Am Mo., 26. Mai 2025 um 10:48 Uhr schrieb Zhaoxun Yan <yan.zhao...@gmail.com >: > Hi All! > I am stuck with this bizarre phenomenon that the http.Error function does > not repond with the customizable text defined by the second argument. > Suppose a simple ill http server with this function that responds an error > as always: > ------------------------------ > func (con *Net) ServeHTTP(w http.ResponseWriter, r *http.Request) { > http.Error(w, "Whatever! I return an error", 403) > return > //the original functional codes are below and they won't get executed now > ...... > } > ================== > So it is easy to transform any of your functional go http code with the > two magic lines and test the functionality of http.Error. > > And here is some javascript that visits a particular url where your > ServeHTTP serves with GET method: > ----------------------------------- > async function get(url){ > let re > try{ > re=await fetch(url,{method: 'GET'}) > }catch(err){ > alert(''The url your provided is not reachable!") > return Promise.reject(1) > } > if (re.status != 200){ > alert(`Server responded with an error:${re.status} ${re.statusText}`) > console.log(`Server responded with an error:${re.status} > ${re.statusText}`) > return Promise.reject(re.status) > }else{ // irrelevant in this situation > return re.json() > } > } > > function test_Error403(){ > get('[where the golang ServeHTTP function serves]'). > then((msg) => console.log(msg))} > ============================= > Then you can link test_Error403() function with a button on an html file, > but be aware to change '[where the golang ServeHTTP function serves]' to > real url. > > The bizarre situation is I always get alert or console log like: > Server responded with an error:403 > while what I expect is: > Server responded with an error:403 Whatever! I return an error > > So is there anything wrong with the syntax? How can I respond the client > with an arbitrary string as `statusText` ? > > Thanks in advance! > > -- > 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+unsubscr...@googlegroups.com. > To view this discussion visit > https://groups.google.com/d/msgid/golang-nuts/CADEX6_VwACq%3DG9hciRr0c1LO3tVFtKs0ebMcvXxvjmdaMx%2Byfw%40mail.gmail.com > <https://groups.google.com/d/msgid/golang-nuts/CADEX6_VwACq%3DG9hciRr0c1LO3tVFtKs0ebMcvXxvjmdaMx%2Byfw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- 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+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CAC4E5Z%3D%3D35LbNoz2zTpnCgiMirTBJDHwSPeePqsyzmVBQfU9Kw%40mail.gmail.com.