/// <summary> /// Returns our standard SingularError for a Bads Request/400. /// </summary> /// <param name="validationException">The validation exception.</param> /// <returns></returns> protected IActionResult BadRequest(BusinessValidationException validationException) { var message = Mapper.Map <FriendlyError>(validationException); //message.CorrelationId = Logger.Log(Log.Severity.Error, Log.Category.Business, validationException); return(SafeNegotiatedContentResult(message.HttpStatusCode, message)); }
public void AddArticle(Article article) { if (article == null) { return; } var customProperties = new Dictionary <string, object> { [nameof(article.Title)] = article.Title, [nameof(article.Author)] = article.Author, [nameof(article.PublicationDate)] = article.PublicationDate }; if (_authorRepository.IsBlackListedAuthor(article.Author)) { throw BusinessValidationException.AuthorIsBlackListed(article.Author, customProperties); } if (_articleRepository.ArticleExists(article)) { throw BusinessException.ArticleExists(article.Title, customProperties); } _articleRepository.AddArticle(article); }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <param name="context"></param> /// <returns></returns> public FriendlyError Convert(BusinessValidationException source, FriendlyError destination, ResolutionContext context) { var validationException = source; ApiErrors apiErrorCode; switch (validationException.ErrorCode) { case BusinessErrors.MissingRequiredField: case BusinessErrors.InaccessibleResource: case BusinessErrors.FieldOutOfRange: apiErrorCode = ApiErrors.InvalidModel; break; case BusinessErrors.NotAuthorized: apiErrorCode = ApiErrors.Unauthorized; break; case BusinessErrors.DependentServiceFailure: apiErrorCode = ApiErrors.Generic; break; case BusinessErrors.ExpectedResourceGone: apiErrorCode = ApiErrors.ExpectedResourceGone; break; case BusinessErrors.ConflictingValue: apiErrorCode = ApiErrors.DuplicateResource; break; case BusinessErrors.PreconditionFailed: apiErrorCode = ApiErrors.PreconditionFailed; break; case BusinessErrors.OperationNotSupported: apiErrorCode = ApiErrors.OperationNotSupported; break; case BusinessErrors.DuplicateName: apiErrorCode = ApiErrors.DuplicateName; break; default: throw new NotImplementedException("Unknown enum " + validationException.ErrorCode); } // create & initialize new dto var result = new FriendlyError { Key = apiErrorCode.GetDisplayName(), Message = apiErrorCode.GetDescription(), DataElement = validationException.DataElement, HttpStatusCode = Convert(apiErrorCode), CorrelationId = Guid.NewGuid().ToString() //logger.ActivityId.ToString() }; result.SetException(validationException); return(result); }
private IActionResult GetBusinessError(string instance, BusinessValidationException e) { return(BadRequest(new ProblemDetails { Title = e.Title, Detail = e.Message, Instance = instance })); }
public static void FillFrom(this ModelStateDictionary modelStateDictionary, BusinessValidationException exception) { Contract.Assert(exception != null); Contract.Assert(modelStateDictionary != null); var pairs = (from info in exception.Errors let property = !string.IsNullOrEmpty(info.PropertyName) ? info.PropertyName : info.ErrorCode let index = property.LastIndexOf('.') let key = index > 0 && index < property.Length ? property.Substring(index + 1) : property select new { key, info.ErrorMessage }).ToList(); foreach (var p in pairs.Where(i => !string.IsNullOrEmpty(i.key))) { // try to match business property name and model property name modelStateDictionary.AddModelError(p.key, p.ErrorMessage); } var messages = pairs.Where(i => string.IsNullOrEmpty(i.key)).Select(i => i.ErrorMessage).ToArray(); if (messages.Length > 0) { modelStateDictionary.AddModelError(string.Empty, string.Join("\r\n", messages)); } }
public Exception HandleException(Exception exception, Guid handlingInstanceId) { var result = new BusinessValidationException(exception.Message, exception); return result; }
public Exception HandleException(Exception exception, Guid handlingInstanceId) { var result = new BusinessValidationException(exception.Message, exception); return(result); }