-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error handling #46
Comments
You can actually use the code in that blog post verbatim—Goji fully supports Perhaps the thing that's tripping you up is the fact that you can't use |
Okay, you are right I was tripping on that, I get it now, thanks for your answer ! :) Do you think this is a good idea btw ?? |
You should (IMO) return something like an Note that if you wanted to return some more context with your error you can |
I personally don't find the burden of calling But YMMV; I haven't had my 10,000 hours with Goji / |
My biggest issue is when you have something like: func SomeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "name")
if err != nil {
http.Error(w, http.StatusText(500), 500)
return // Forget this, and our handler won't explicitly return, causing unintended side effects
}
err := db.GetThing(thing)
http.Error(w, http.StatusText(500), 500)
return // Same again
}
} Since the compiler does not enforce naked returns on functions, your handler will continue, writing multiple response bodies and otherwise doing things you may not want. I typically only have 3-4 error returns in each handler, but across ~14 handlers (thus far) the potential for a typo starts to creep in. My I can also take a shortcut and write Still, this is why I like Go (it's flexible, but it's easy to understand) — hopefully the issue author finds this useful as well ;) |
http.Error(w, http.StatusText(500), 500)
return // Forget this, and our handler won't explicitly return, causing unintended side effects Maybe that's a good candidate for new check in GoLint or go vet? |
Okay thank you guys for those infos, very interesting I will try everything here. Still, if you do : http.Error(w, http.StatusText(500), 500)
return in a middleware handler, can it stop the execution and return an error ? And in the code of the blog post they do : c := appengine.NewContext(r) // I guess it's the full context of the request with some cool infos
c.Errorf("%v", e.Error) Is there some simple way to get the full context of the request in goji too to log it ? (Like the IP of the user on the other side, the time, the url, the parameters, the path, the name of the matched route ( can I name a route ?), and more ? To finish anoying you ; I use gorm, and when nothing is found it returns an error, it's not such a big error but in this case I was doing something like this : func VerifyErrors(query *gorm.DB) {
if query.Error != gorm.RecordNotFound { // Pardon me this is my first application !!!!
log.Panicf("gorm error : '%v'", query.Error)
}
} The more I look at it the worst it looks like. |
@Azer-
|
Awesome, thanks @elithrar :) I'll test all this !! I need more exp ! |
@Azer- Perhaps the use of panic + recoverer-middleware might not be a good approach for control flow. Like @elithrar mentioned, it would be ideal to return an error back to the handler. Reserve a panic for the truly exceptional cases I personally found @mmcgrana 's function to handle response interesting ~ https://github.com/mmcgrana/pgpin/blob/master/web.go#L39 It assumes JSON output and the same function is used for returning error and data - but worth taking a look |
Hey @saj1th, yes very interesting indeed, thanks :) |
Since this issue hasn't seen any traffic in over a month I'm going to close it out. Feel free to open a new issue if you have any more questions—I'm always curious to see what kind of things people run into when using Goji! |
Hello there !
I like goji, it's all simple and light and I have a question : How would you do simple error handling ?
Like, may be in this post : http://blog.golang.org/error-handling-and-go ?
Having some handler funcs that look like this :
With appError looking like this :
So then in the handler I could :
Thanks !
The text was updated successfully, but these errors were encountered: