Guest User

Untitled

a guest
Sep 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. public class ApiExceptionHandler : IExceptionHandler
  2. {
  3. public async Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
  4. {
  5.  
  6. var exceptionType = context.Exception.GetType();
  7.  
  8. if (exceptionType == typeof(ResourceNotFoundException))
  9. {
  10. context.Result = new ResponseMessageResult(
  11. context.Request.CreateResponse(HttpStatusCode.NotFound, context.Exception.Message));
  12. }
  13.  
  14. else if (exceptionType == typeof(UserNotFoundException))
  15. {
  16. context.Result = new ResponseMessageResult(
  17. context.Request.CreateResponse(HttpStatusCode.Unauthorized, context.Exception.Message));
  18. }
  19.  
  20. else if (exceptionType == typeof(UserAlreadyExistsExeption))
  21. {
  22. context.Result = new ResponseMessageResult(
  23. context.Request.CreateResponse(HttpStatusCode.Conflict, context.Exception.Message));
  24. }
  25.  
  26. ...
  27.  
  28. else
  29. {
  30. context.Result = new ResponseMessageResult(
  31. context.Request.CreateResponse(HttpStatusCode.InternalServerError, "An unexpected error occured"));
  32. }
  33. }
  34. }
  35.  
  36. public class UserAlreadyExistsExeption: Exception
  37. {
  38. public UserAlreadyExistsExeption()
  39. {
  40. }
  41.  
  42. public UserAlreadyExistsExeption(string message) : base(message)
  43. {
  44. }
  45.  
  46. public UserAlreadyExistsExeption(string message, Exception inner) : base(message, inner)
  47. {
  48.  
  49. }
  50. }
  51.  
  52. //If user exists
  53. throw new UserAlreadyExistsExeption("User already exists");
  54.  
  55. public interface IHasHttpErrorCode
  56. {
  57. HttpStatusCode GetHttpStatusCode();
  58. }
  59.  
  60. public class UserAlreadyExistsExeption: Exception, IHasHttpErrorCode
  61. {
  62. public UserAlreadyExistsExeption()
  63. {
  64. }
  65.  
  66. public UserAlreadyExistsExeption(string message) : base(message)
  67. {
  68. }
  69.  
  70. public UserAlreadyExistsExeption(string message, Exception inner) : base(message, inner)
  71. {
  72.  
  73. }
  74.  
  75. public HttpStatusCode GetHttpStatusCode() {
  76. return HttpStatusCode.Conflict;
  77. }
  78. }
  79.  
  80. public class ApiExceptionHandler : IExceptionHandler
  81. {
  82. public async Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
  83. {
  84.  
  85. var exceptionType = context.Exception as IHasHttpErrorCode;
  86.  
  87.  
  88. if (customException != null) {
  89. context.Result = new ResponseMessageResult(
  90. context.Request.CreateResponse(customException.GetHttpStatusCode(), context.Exception.Message));
  91. }
  92. else
  93. {
  94. context.Result = new ResponseMessageResult(
  95. context.Request.CreateResponse(HttpStatusCode.InternalServerError, "An unexpected error occured"));
  96. }
  97. }
  98. }
  99.  
  100. public abstract class ServerException: Exception {
  101. // standard constructors...
  102. public abstract HttpStatusCode GetHttpStatusCode();
  103. }
  104.  
  105. public class UserAlreadyExistsExeption: ServerException {
  106. // again standard .ctors
  107. override public HttpStatusCode GetHttpStatusCode() => HttpStatusCode.Conflict;
  108. }
  109.  
  110. public class UserAlreadyExistsExeption: Exception, IHasHttpErrorCode
  111. {
  112. public UserAlreadyExistsExeption(): base("User already exists") {}
  113. }
  114.  
  115. throw new UserAlreadyExistsExeption();
  116.  
  117. throw new UserAlreadyExistsExeption("User already exists");
Add Comment
Please, Sign In to add comment