private static void HandleValidationException(Exception exception, ErrorResponce errorDto)
        {
            {
                var ex = (ValidationException)exception;

                errorDto.Message = ex.Message;
                errorDto.Errors  = ex.Failures.Select(x => x.Value)
                                   .SelectMany(x => x)
                                   .Distinct()
                                   .ToList();
            }
        }
        private static void HandleApplicationException(Exception exception, ErrorResponce errorDto)
        {
            var failures = exception.WithFailures().ToList();

            if (!failures.Any())
            {
                return;
            }

            errorDto.Message = failures.FirstOrDefault();
            failures.RemoveAt(0);
            errorDto.Errors = failures;
        }
        private async Task WriteResponce(ErrorResponce responseObj, HttpContext context, HttpStatusCode statusCode)
        {
            var response = context.Response;

            var content = JsonConvert.SerializeObject(responseObj, new JsonSerializerSettings()
            {
                ContractResolver = new DefaultContractResolver()
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            });

            response.ContentType = "application/json";

            response.StatusCode = (int)statusCode;

            await response.WriteAsync(content);
        }
        private async Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            var statusCode = HttpStatusCode.BadRequest;
            var errorDto   = new ErrorResponce();

            if (exception is ValidationException)
            {
                HandleValidationException(exception, errorDto);
            }
            else if (exception is System.Security.Authentication.InvalidCredentialException)
            {
                HandleInvalidCredential(errorDto);
            }
            else
            {
                HandleApplicationException(exception, errorDto);
            }

            GetResponseStatus(exception, ref statusCode);

            await WriteResponce(errorDto, context, statusCode);
        }
 private void HandleInvalidCredential(ErrorResponce errorDto)
 {
     errorDto.Message = "Invalid username or password.";
 }