Handling Errors

version: 3.1

# Create an exception

This is just a basic exception class

public class HttpResponseException:Exception
    {
        public HttpResponseException(string value)
        {
            Value = value;
        }
        public int Status { get; set; } = 400;  // The http response code status
        public string Value { get; set; }   // The message we want to send
    }
1
2
3
4
5
6
7
8
9

# Create a filter for the exception

    class HttpResponseExceptionFilter : IActionFilter, IOrderedFilter
    {
        // This method will be called after the action is excecuted
        public void OnActionExecuted(ActionExecutedContext context)
        {
            // if the exception is an HttpResponseException
            if (context.Exception is HttpResponseException exception)
            {
                // set the result
                context.Result = new ObjectResult(exception.Value)
                {
                    StatusCode = exception.Status,

                };
                context.ExceptionHandled = true;
            }
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            // nothing here
        }

        // The order which this Filter will be excecuted
        public int Order { get; } = 1000;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# Register the filter

 services.AddControllers(
            options => {
                options.Filters.Add(new HttpResponseExceptionFilter());
            }
        )
1
2
3
4
5

# Throw the exception

 public UserModel GetById(int id){

     // get the user
     var user = _context.Users.SingleOrDefault(u => u.Id == id);

     if(!user){ // if we didn't find the user throw an exception
         throw new HttpResponseException("Couldn't find user with that Id")
     }
 }
1
2
3
4
5
6
7
8
9