Error Handling
If validation or business errors occur, a 422 Unprocessable Entity response is returned, containing a list of Message elements with error messages and their corresponding 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 -> message list (Text, Code)
}
else if (response.IsSuccessStatusCode)
{
//ok
}
else
{
var raw = await response.Content.ReadAsStringAsync();
// Generic error handling (400, 500, etc.)
}