chain.of.responsibility.example's People
chain.of.responsibility.example's Issues
Exception handling in chain of res
When I convert methods to async/await, 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?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.