split encode & decode error handling and raise proper exceptions (thanks @hodgestar)