Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpError"/> class for <paramref name="modelState"/>.
        /// </summary>
        /// <param name="modelState">The invalid model state to use for error information.</param>
        /// <param name="includeErrorDetail"><c>true</c> to include exception messages in the error; <c>false</c> otherwise</param>
        public HttpError(ModelStateDictionary modelState, bool includeErrorDetail) : this()
        {
            if (modelState == null)
            {
                throw Error.ArgumentNull("modelState");
            }

            if (modelState.IsValid)
            {
                throw Error.Argument("modelState", SRResources.ValidModelState);
            }

            Message = SRResources.BadRequest;

            HttpError modelStateError = new HttpError();

            foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState)
            {
                string key = keyModelStatePair.Key;
                ModelErrorCollection errors = keyModelStatePair.Value.Errors;
                if (errors != null && errors.Count > 0)
                {
                    IEnumerable <string> errorMessages = errors
                                                         .Select(
                        error =>
                    {
                        if (includeErrorDetail && error.Exception != null)
                        {
                            return(error.Exception.Message);
                        }
                        else
                        {
                            return(String.IsNullOrEmpty(error.ErrorMessage)
                                      ? SRResources.ErrorOccurred
                                      : error.ErrorMessage);
                        }
                    }
                        )
                                                         .ToArray();
                    modelStateError.Add(key, errorMessages);
                }
            }

            Add(HttpErrorKeys.ModelStateKey, modelStateError);
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpError"/> class for <paramref name="modelState"/>.
        /// </summary>
        /// <param name="modelState">The invalid model state to use for error information.</param>
        /// <param name="includeErrorDetail">
        /// <c>true</c> to include exception messages in the error; <c>false</c> otherwise.
        /// </param>
        public HttpError(ModelStateDictionary modelState, bool includeErrorDetail)
            : this()
        {
            if (modelState == null)
            {
                throw new ArgumentNullException(nameof(modelState));
            }

            if (modelState.IsValid)
            {
                throw new ArgumentException(ShimResources.HttpError_ValidModelState, nameof(modelState));
            }

            Message = ShimResources.HttpError_BadRequest;

            var modelStateError = new HttpError();

            foreach (KeyValuePair <string, ModelStateEntry> keyModelStatePair in modelState)
            {
                var key    = keyModelStatePair.Key;
                var errors = keyModelStatePair.Value.Errors;
                if (errors != null && errors.Count > 0)
                {
                    var errorMessages = errors.Select(error =>
                    {
                        if (includeErrorDetail && error.Exception != null)
                        {
                            return(error.Exception.Message);
                        }
                        else
                        {
                            return
                            (string.IsNullOrEmpty(error.ErrorMessage) ?
                             ShimResources.HttpError_GenericError :
                             error.ErrorMessage);
                        }
                    }).ToArray();
                    modelStateError.Add(key, errorMessages);
                }
            }

            Add(HttpErrorKeys.ModelStateKey, modelStateError);
        }
Example #3
0
 internal static HttpError AddAndReturn(this HttpError httpError, string key, object value)
 {
     httpError.Add(key, value);
     return(httpError);
 }