private void HandleException(HttpContext httpContext, string referenceNo) { BaseResponseDto <object> baseResponseDto = new BaseResponseDto <object>(); Exception exception = httpContext.Features.Get <IExceptionHandlerFeature>()?.Error; CommonException customExcep = null; if (exception is ValidationException) { customExcep = (ValidationException)exception; } if (exception is PermissionException) { customExcep = (PermissionException)exception; } if (exception is BusinessException) { customExcep = (BusinessException)exception; } if (exception is RepositoryException) { customExcep = (RepositoryException)exception; } if (customExcep != null) { baseResponseDto.ReturnError(customExcep.Result, exception.Message, referenceNo, ((PermissionException)exception).Errors); } httpContext.Response.ContentType = "application/json"; httpContext.Response.StatusCode = (int)HttpStatusCode.OK; httpContext.Response.WriteAsync(baseResponseDto.ToString()); }
public async Task <JsonContentResult> Handle <TRequest, TResponse>(Func <BaseRequestDto <TRequest>, Task <BaseResponseDto <TResponse> > > useCaseRequest, BaseRequestDto <TRequest> request) { ValidationResult validationResult = ValidateRequestDto(request); BaseResponseDto <TResponse> response = new BaseResponseDto <TResponse>(); if (validationResult.IsValid) { response = await useCaseRequest.Invoke(request); if (response != null) { ContentResult.StatusCode = (int)HttpStatusCode.OK; ContentResult.Content = JsonHandler.Serialize(response); } } else { Dictionary <string, List <string> > dict = new Dictionary <string, List <string> >(); validationResult.Errors.GroupBy(p => p.PropertyName).ToList().ForEach(item => dict.Add(item.Key, item.Select(e => e.ErrorMessage).ToList())); response.ReturnError(HttpEnum.ResponseStatus.InvalidData, HttpEnum.ResponseStatus.InvalidData.ToString(), Guid.NewGuid().ToString(), propErrors: dict); } ContentResult.StatusCode = (int)HttpStatusCode.OK; ContentResult.Content = JsonHandler.Serialize(response); return(ContentResult); }