/// <summary>
        /// Used to throw validation exceptions
        /// </summary>
        /// <param name="modelState"></param>
        /// <param name="content"></param>
        /// <param name="linkTemplate"></param>
        /// <param name="message"></param>
        /// <param name="id"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        protected ModelValidationException ValidationException <TRepresentation>(
            ModelStateDictionary modelState,
            TRepresentation content,
            Link linkTemplate,
            string message = null, int?id = null, params string[] errors)
        {
            //var metaDataProvider = Configuration.Services.GetModelMetadataProvider();
            var errorList = new List <ValidationErrorRepresentation>();

            foreach (KeyValuePair <string, ModelState> ms in modelState)
            {
                foreach (var error in ms.Value.Errors)
                {
                    //TODO: We could try to work around this but i think it's such a small thing that it's not worth spending the time fixing right now

                    ////hack - because webapi doesn't seem to support an easy way to change the model metadata for a class, we have to manually
                    //// go get the 'display' name from the metadata for the property and use that for the logref otherwise we end up with the c#
                    //// property name (i.e. contentTypeAlias vs ContentTypeAlias). I'm sure there's some webapi way to achieve
                    //// this by customizing the model metadata but it's not as clear as with MVC which has IMetadataAware attribute
                    var logRef = ms.Key;
                    //var parts = ms.Key.Split('.');
                    //var isContentField = parts.Length == 2 && parts[0] == "content";
                    //if (isContentField)
                    //{
                    //    parts[1] = metaDataProvider.GetMetadataForProperty(() => content, typeof (ContentRepresentation), parts[1])
                    //                .GetDisplayName();
                    //    logRef = string.Join(".", parts);
                    //}

                    errorList.Add(new ValidationErrorRepresentation
                    {
                        LogRef  = logRef,
                        Message = error.ErrorMessage
                    });
                }
            }

            //add additional messages
            foreach (var error in errors)
            {
                errorList.Add(new ValidationErrorRepresentation {
                    Message = error
                });
            }

            var errorModel = new ValidationErrorListRepresentation(errorList, linkTemplate, id)
            {
                HttpStatus = (int)HttpStatusCode.BadRequest,
                Message    = message ?? "Validation errors occurred"
            };

            return(new ModelValidationException(errorModel));
        }
Ejemplo n.º 2
0
 public ModelValidationException(ValidationErrorListRepresentation errors, int?id = null)
 {
     Errors = errors;
     Id     = id;
 }
 public ModelValidationException(ValidationErrorListRepresentation errors, int? id = null)
 {
     Errors = errors;
     Id = id;
 }