Returning consistent errors in .NET Web API

Sebastian Meckovski 25 Reputation points
2024-04-25T13:01:35.0233333+00:00

I have a backend .NET Core Web API application and React App on the front. I would like to be able to return errors from the API and display them in the app. The problem I'm facing is that some classes return errors differently. For example, I'm using System.ComponentModel.DataAnnotations to validate form fields.


using System.ComponentModel.DataAnnotations;

namespace SportAdvisorAPI.Models
{
    public class RegisterUserDto
    {
        [Required]
        public required string Name { get; set; }
        [Required]
        [EmailAddress]
        public required string Email { get; set; }
        [Required]
        public required string Password { get; set; }
        [Required]
        public required string ConfirmPassword { get; set; }

    }
}

So if the form validation fails the API response I get will look like this:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-3d6fda7d7ab4e861795de4091bae0e40-859fd543f9959150-00",
    "errors": {
        "Name": [
            "The Name field is required."
        ],
        "Email": [
            "The Email field is required.",
            "The Email field is not a valid e-mail address."
        ]
    }
}

I'm also using UserManager class for user registration, and if that fails the API response returned look like this:

{
    "succeeded": false,
    "errors": [
        {
            "code": "PasswordTooShort",
            "description": "Passwords must be at least 6 characters."
        },
        {
            "code": "PasswordRequiresNonAlphanumeric",
            "description": "Passwords must have at least one non alphanumeric character."
        },
        {
            "code": "PasswordRequiresLower",
            "description": "Passwords must have at least one lowercase ('a'-'z')."
        },
        {
            "code": "PasswordRequiresUpper",
            "description": "Passwords must have at least one uppercase ('A'-'Z')."
        }
    ]
}    

So if I want to display those error messages in my React app I'd need to write an extra logic to check what errorsobject looks like and remap it to something usable. Ideally I want a simple array of strings, would make it a lot simpler to display it in the app regardless of what error I'd be getting.

I there a way on the .NET API side to globally configure what those error responses would look like? Maybe there's a better way of doing this that I'm not aware of?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,396 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,188 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
299 questions
{count} votes