Split server and client error handling, and make it possible to inject a handler for client errors.

Relates to #1.