factor out error response building