Catch exceptions in the API server and report them to clients

This should be merged after the other PR @christian-marie is about to merge.