using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; using System.Runtime; using System.Runtime.Serialization; namespace Sleis.Validation.Spring { public class ValidationException : Exception, ISerializable { public string ErrorMessage { get; set; } public ValidationErrorType ErrorType { get; set; } public List InnerErrors { get; set; } public ValidationException(List errors, string format, params object[] args) : this(errors, String.Format(format, args)) { } public ValidationException(List errors, string message) : base(message) { InnerErrors = errors; ErrorMessage = message; } public ValidationException(ValidationErrorType type, string format, params object[] args) : this(type, String.Format(format, args)) { } public ValidationException(ValidationErrorType type, string name) : base(name) { ErrorType = type; switch (type) { case ValidationErrorType.Required: ErrorMessage = String.Format("{0} is required", name); break; case ValidationErrorType.Format: ErrorMessage = String.Format("{0} has an invalid format", name); break; case ValidationErrorType.Conversion: ErrorMessage = String.Format("{0} cannot be converted to the required format", name); break; default: ErrorMessage = name; break; } } void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); info.AddValue("ErrorType", ErrorType); info.AddValue("ErrorMessage", ErrorMessage); info.AddValue("InnerErrors", InnerErrors); } } public enum ValidationErrorType : short { Simple, Required, Format, Conversion } }