Return appropriate status code on error