3 ms·
I am writing a large Go web application right now and I disagree with this. I do not want to return an error. I want to write an error page or an error JSON mes
by carom 4y ago
I am writing a large Go web application right now and I disagree with this. I do not want to return an error. I want to write an error page or an error JSON message depending on the handler. By the time you are deep in a web application you are copy and pasting all your error blocks anyway. This [1] is a sample of one of my functions. I honestly love Go for how consistent it is.
I wrote a rudimentary static checker [2] to make sure that any function does not call both the jsonError() and errorPage() functions. This was quite a problem given how much I reuse code. I disagree with the author's take because it is incredibly obvious and quite easy to track down when you forget a return. You will have a big block of garbage in the middle of your page and JSON, and your unique error message should lead you directly to the line.
My problem is actually in executing the templates at the end of the functions [1], when you finally write out the page. If that has an error, you can't really back track. I've been thinking about changing that to write to a temporary buffer which then writes to the http.ResponseWriter but haven't made the change yet.
1. https://gist.github.com/TACIXAT/24621013248e2d91bebd16c5a02a99f9 https://gist.github.com/TACIXAT/24621013248e2d91bebd16c5a02a...
2. https://gist.github.com/TACIXAT/1000e7d873f0a8a738b3e720b4a7684c https://gist.github.com/TACIXAT/1000e7d873f0a8a738b3e720b4a7...