Лучшая практика для возврата ошибок в ASP.NET Web API

У меня есть опасения по поводу того, как мы возвращаем ошибки клиенту.

Мы немедленно возвращаем ошибку, бросаяHttpResponseException когда мы получаем ошибку:

public void Post(Customer customer)
{
    if (string.IsNullOrEmpty(customer.Name))
    {
        throw new HttpResponseException("Customer Name cannot be empty", HttpStatusCode.BadRequest) 
    }
    if (customer.Accounts.Count == 0)
    {
         throw new HttpResponseException("Customer does not have any account", HttpStatusCode.BadRequest) 
    }
}

Или мы накапливаем все ошибки и отправляем обратно клиенту:

public void Post(Customer customer)
{
    List<string> errors = new List<string>();
    if (string.IsNullOrEmpty(customer.Name))
    {
        errors.Add("Customer Name cannot be empty"); 
    }
    if (customer.Accounts.Count == 0)
    {
         errors.Add("Customer does not have any account"); 
    }
    var responseMessage = new HttpResponseMessage<List<string>>(errors, HttpStatusCode.BadRequest);
    throw new HttpResponseException(responseMessage);
}

Это просто пример кода, не имеет значения ни ошибки валидации, ни ошибки сервера, я просто хотел бы узнать лучшие практики, плюсы и минусы каждого подхода.

Ответы на вопрос(11)

Ваш ответ на вопрос