Convert error types to typedef'ed enums

I am proposing that error types be changed from `int16_t` to typedef'ed enums to improve type-checking and enhance editing and debugging. Benefits include:

* compile-time errors
* better completion in editors
* the ability for the debugger to show the constant instead of the raw value

Since the change involves a binary ABI change, the `int16_t`-based types can be retained by defining the macro `LEGACY_ERRORS`.

The process of making these changes revealed a few errors in the library, such as the first of the two commits. If the request is rejected, please review the diff to see if those changes should be made regardless of accepting this pull request.