public BadRequestWithErrorsResult(Exception ex)
 {
     Errors = new ErrorDetailCollection();
     if (ex is ExceptionBase)
     {
         Errors.Add(new ErrorDetail((ExceptionBase)ex));
     }
     else
     {
         Errors.Add(new ErrorDetail(HttpStatusCode.BadRequest, "", Util.AnErrorHasOccured));
     }
 }
Example #2
0
        /// <summary>
        /// Formats the detail collection from a service exception.
        /// </summary>
        /// <param name="errorDetails"></param>
        /// <returns></returns>
        private static string GenerateOrgErrorDetailsInfo(ErrorDetailCollection errorDetails)
        {
            if (errorDetails != null && errorDetails.Count > 0)
            {
                StringBuilder sw = new StringBuilder();
                sw.AppendLine("Error Details\t:");
                foreach (var itm in errorDetails)
                {
                    string valueText = itm.Value != null?itm.Value.ToString() : "Not Set";

                    sw.AppendLine($"{itm.Key}\t: {valueText}");
                }
                return(sw.ToString());
            }
            return(string.Empty);
        }
        public BadRequestWithErrorsResult(ModelStateDictionary modelStateDictionary, string errorMessage = null)
        {
            Errors = new ErrorDetailCollection();
            foreach (var key in modelStateDictionary.Keys)
            {
                var modelStateItem = modelStateDictionary[key];
                var stringErrors   = string.Join("<br/>",
                                                 modelStateItem.Errors.Where(m => m.ErrorMessage.IsNotEmpty()).Select(m => m.ErrorMessage).Distinct());
                var error = new ErrorDetail(HttpStatusCode.BadRequest, key, stringErrors);
                Errors.Add(error);
            }

            if (!Errors.Any(m => m.Key == string.Empty))
            {
                Errors.Insert(0, new ErrorDetail(HttpStatusCode.BadRequest, "", errorMessage ?? Util.ValidationIssues));
            }
        }