Handle custom error codes

Fixes #151