Move error handling to the endpoint