4

Can mgo return error different than QueryError or ErrNotFound? What with database connection errors?

Is it a good practise to panic on error different than ErrNotFound and recover on the top of http handlers stack with something like pretty response with status 500?

1
  • I've posted a basic answer but @gustavo-niemeyer (mgo author) may be able to provide more definite info. Commented Oct 9, 2013 at 10:08

2 Answers 2

5

The set of errors returned by mgo is not constrained, because it does a number of operations underneath that may also return errors (DNS resolution, connection establishment, timeouts, etc). So the proper way to handle errors with mgo is the same as most places: handle the ones you do know about and have custom logic for, and bail out on the ones you don't. Good bailing out encompasses undoing any local side-effects (close/remove locally created files, etc), and then returning the error to the caller, perhaps decorated or wrapped with custom context information.

I wouldn't panic on such errors. Panics are usually for abnormal situations, when the developer did something wrong with the API, or the environment is seriously damaged and the best course of action is to stop altogether, for example. A connection with the database (or anything network related) should be expected to fall down every once in a while, and handled appropriately rather than just logging an undistinguishable crash.

If you have more details and would like to talk further, please come over to the mailing list.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I see now that it's hard to build pretty ORM-like layer in Go like ActiveRecord in Rails and I moved from "callback hell" in Node.js to "error hell" in Go. Am I missing something?
@RafałSobota If you construct your functions properly in order to return any errors that are encountered in the execution of that function then you just pass them up the chain until they are handled (often in the main loop, but sometimes not). The errors are all of one type and usually contain very useful information. If you need to construct your own, it's very easy with errors.New golang.org/pkg/errors/#New . There should be no need to go into "error hell", have a look at the Go source for examples: golang.org/src/pkg .
3

I believe you can check any error with LastError. Most of the error returning functions return a standard Go error that should be checked upon function return.

Usually, in Go, you'd want some very special use case before resorting to panic / recover. It's best practice to handle the errors as they arise.

For more info see Error handling and Go and Defer, Panic, and Recover from The Go Blog.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.