Saltar a contenido

Tratamiento de Errores

Si se producen errores de validación o de negocio, se retorna 422 Unprocessable Entity con una lista de elementos Message con mensajes de error y ExceptionCodes.

public class MessageDto
{
    public string Text { get; set; } = string.Empty;
    public int Code { get; set; }
}

using System.Net;
using System.Net.Http.Json;

var response = await httpClient.PostAsJsonAsync("api/v2/...", request);

if (response.StatusCode == HttpStatusCode.UnprocessableEntity)
{
    var errors = await response.Content.ReadFromJsonAsync<List<MessageDto>>() ?? [];
    // errors -> lista de mensajes (Text, Code)
}
else if (response.IsSuccessStatusCode)
{
    //ok
}
else
{
    var raw = await response.Content.ReadAsStringAsync();
    // Manejo genérico de error (400, 500, etc.) 
}
 [
    {
        "Code": integer,
        "Text": "string"
    }
    ,  ...
]