Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ApiExceptionHandler : IExceptionHandler
- {
- public async Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
- {
- var exceptionType = context.Exception.GetType();
- if (exceptionType == typeof(ResourceNotFoundException))
- {
- context.Result = new ResponseMessageResult(
- context.Request.CreateResponse(HttpStatusCode.NotFound, context.Exception.Message));
- }
- else if (exceptionType == typeof(UserNotFoundException))
- {
- context.Result = new ResponseMessageResult(
- context.Request.CreateResponse(HttpStatusCode.Unauthorized, context.Exception.Message));
- }
- else if (exceptionType == typeof(UserAlreadyExistsExeption))
- {
- context.Result = new ResponseMessageResult(
- context.Request.CreateResponse(HttpStatusCode.Conflict, context.Exception.Message));
- }
- ...
- else
- {
- context.Result = new ResponseMessageResult(
- context.Request.CreateResponse(HttpStatusCode.InternalServerError, "An unexpected error occured"));
- }
- }
- }
- public class UserAlreadyExistsExeption: Exception
- {
- public UserAlreadyExistsExeption()
- {
- }
- public UserAlreadyExistsExeption(string message) : base(message)
- {
- }
- public UserAlreadyExistsExeption(string message, Exception inner) : base(message, inner)
- {
- }
- }
- //If user exists
- throw new UserAlreadyExistsExeption("User already exists");
- public interface IHasHttpErrorCode
- {
- HttpStatusCode GetHttpStatusCode();
- }
- public class UserAlreadyExistsExeption: Exception, IHasHttpErrorCode
- {
- public UserAlreadyExistsExeption()
- {
- }
- public UserAlreadyExistsExeption(string message) : base(message)
- {
- }
- public UserAlreadyExistsExeption(string message, Exception inner) : base(message, inner)
- {
- }
- public HttpStatusCode GetHttpStatusCode() {
- return HttpStatusCode.Conflict;
- }
- }
- public class ApiExceptionHandler : IExceptionHandler
- {
- public async Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
- {
- var exceptionType = context.Exception as IHasHttpErrorCode;
- if (customException != null) {
- context.Result = new ResponseMessageResult(
- context.Request.CreateResponse(customException.GetHttpStatusCode(), context.Exception.Message));
- }
- else
- {
- context.Result = new ResponseMessageResult(
- context.Request.CreateResponse(HttpStatusCode.InternalServerError, "An unexpected error occured"));
- }
- }
- }
- public abstract class ServerException: Exception {
- // standard constructors...
- public abstract HttpStatusCode GetHttpStatusCode();
- }
- public class UserAlreadyExistsExeption: ServerException {
- // again standard .ctors
- override public HttpStatusCode GetHttpStatusCode() => HttpStatusCode.Conflict;
- }
- public class UserAlreadyExistsExeption: Exception, IHasHttpErrorCode
- {
- public UserAlreadyExistsExeption(): base("User already exists") {}
- }
- throw new UserAlreadyExistsExeption();
- throw new UserAlreadyExistsExeption("User already exists");
Add Comment
Please, Sign In to add comment