samedi 10 juin 2023

Chain of Responsibility with async awaite method and exception handling in c#

I have Some async and await method if an error occurs, instead of throw an error, it just exits the handler and continues processing the rest of the methods in that service for example this is the handler

using System.Threading.Tasks;

namespace Chain.Of.Responsibility.Example.Chain.Pattern
{
    public abstract class Handler<T>
    {
        private IHandler<T> Next { get; set; }

        public virtual async Task Handle(T request)
        {
            Next?.Handle(request);
           
        }

        public IHandler<T> SetNext(IHandler<T> next)
        {
            Next = next;
            return Next;
        }
    }
}

and this is UsernameRequiredValidationHandler

using System; using System.Threading.Tasks;

namespace Chain.Of.Responsibility.Example.Chain.Pattern
{
    public class UsernameRequiredValidationHandler : Handler<User>, IHandler<User>
    {
        public override async Task Handle(User user)
        {
            if (string.IsNullOrWhiteSpace(user.Username))
            {
                throw new Exception("Username is required.");
            }
            base.Handle(user);
        }
    }
}

and this is PasswordRequiredValidationHandler

using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace Chain.Of.Responsibility.Example.Chain.Pattern
{
    public class PasswordRequiredValidationHandler : Handler<User>, IHandler<User>
    {
        public override async Task Handle(User user)
        {
          
            if (string.IsNullOrWhiteSpace(user.Password))
            {
                throw new Exception("Password is required.");
            }

             base.Handle(user);
        }
    }
}

and this is OnlyAdultValidationHandler

using System;
using System.Threading.Tasks;

namespace Chain.Of.Responsibility.Example.Chain.Pattern
{
    public class OnlyAdultValidationHandler : Handler<User>, IHandler<User>
    {
        public override async Task Handle(User user)
        {
            throw new Exception();
            if (user.BirthDate.Year > DateTime.Now.Year - 18)
            {
                throw new Exception("Age under 18 is not allowed.");
            }

            base.Handle(user);
        }
    }
}

and when exception throw in OnlyAdultValidationHandler the exception not catch in try catch block in the Register class

public class ChainPatternRegistrationProcessor
    {
        public async Task Register(User user)
        {
            try
            {
                var handler = new UsernameRequiredValidationHandler();
                handler.SetNext(new PasswordRequiredValidationHandler())
                    .SetNext(new OnlyAdultValidationHandler());

                await handler.Handle(user);
                Console.WriteLine("");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }

can we catch this error in Register class?

Aucun commentaire:

Enregistrer un commentaire