Return errors rather than doing panics.

This makes it simpler to handle errors in code importing this package.
Not sure this needs to be in the external API. I can't really think of a case where the client would care to check against this value.