示例#1
0
        /// <summary>
        ///     Builds an <see cref="DynamicErrorObject" /> from an exception along with all inner exceptions.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <returns>DynamicErrorObject.</returns>
        public static DynamicErrorObject ToDynamicDataObject(this Exception ex)
        {
            var     ret = new DynamicErrorObject("Error Details", ex.Message);
            dynamic d   = ret;

            d.Type = ex.GetType().Name;

            if (ex.InnerException is null)
            {
                return(ret);
            }

            ret.Title = "Please refer to the error property for additional information.";
            ret.Errors.Add(ex.InnerException.ToDynamicDataObject());

            return(ret);
        }
        /// <inheritdoc />
        /// <summary>
        ///     Called when just prior to
        ///     <see cref="M:APIBlox.AspNetCore.ActionResults.ProblemResult.OnFormatting(Microsoft.AspNetCore.Mvc.ActionContext)" />
        ///     completes.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="obj">The object.</param>
        protected override void OnBeforeFormattingComplete(ActionContext context, RequestErrorObject obj)
        {
            var ms = context.ModelState;

            if (ms.IsValid)
            {
                throw new ArgumentException(
                          "The validation state is VALID, the action result " +
                          $"{nameof(ValidationFailureResult)} is being inappropriately used."
                          );
            }

            var errors = context.ModelState.Keys
                         .Where(k => !k.IsEmptyNullOrWhiteSpace())
                         .SelectMany(key =>
                                     ms[key].Errors.Select(x =>
            {
                dynamic ret = new DynamicErrorObject
                {
                    Title  = "Invalid Property Value",
                    Detail = x.ErrorMessage
                };
                ret.Property = key;

                return(ret);
            }
                                                           )
                                     ).ToList();

            if (!errors.Any())
            {
                obj.Detail = "Errors have occured.";
            }

            foreach (var err in errors)
            {
                obj.Errors.Add(err);
            }
        }