// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Web.Http.Properties; namespace System.Web.Http { /// /// Utility class for creating and unwrapping instances. /// [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Error", Justification = "This usage is okay.")] internal static class Error { /// /// Formats the specified resource string using . /// /// A composite format string. /// An object array that contains zero or more objects to format. /// The formatted string. [SuppressMessage("Microsoft.Naming", "CA1719:ParameterNamesShouldNotMatchMemberNames", MessageId = "0#", Justification = "Standard String.Format pattern and names.")] internal static string Format(string format, params object[] args) { return String.Format(CultureInfo.CurrentCulture, format, args); } /// /// Creates an with the provided properties. /// /// A composite format string explaining the reason for the exception. /// An object array that contains zero or more objects to format. /// The logged . internal static ArgumentException Argument(string messageFormat, params object[] messageArgs) { return new ArgumentException(Error.Format(messageFormat, messageArgs)); } /// /// Creates an with the provided properties. /// /// The name of the parameter that caused the current exception. /// A composite format string explaining the reason for the exception. /// An object array that contains zero or more objects to format. /// The logged . internal static ArgumentException Argument(string parameterName, string messageFormat, params object[] messageArgs) { return new ArgumentException(Error.Format(messageFormat, messageArgs), parameterName); } /// /// Creates an with a message saying that the argument must be an "http" or "https" URI. /// /// The name of the parameter that caused the current exception. /// The value of the argument that causes this exception. /// The logged . internal static ArgumentException ArgumentUriNotHttpOrHttpsScheme(string parameterName, Uri actualValue) { return new ArgumentException(Error.Format(CommonWebApiResources.ArgumentInvalidHttpUriScheme, actualValue, Uri.UriSchemeHttp, Uri.UriSchemeHttps), parameterName); } /// /// Creates an with a message saying that the argument must be an absolute URI. /// /// The name of the parameter that caused the current exception. /// The value of the argument that causes this exception. /// The logged . internal static ArgumentException ArgumentUriNotAbsolute(string parameterName, Uri actualValue) { return new ArgumentException(Error.Format(CommonWebApiResources.ArgumentInvalidAbsoluteUri, actualValue), parameterName); } /// /// Creates an with a message saying that the argument must be an absolute URI /// without a query or fragment identifier and then logs it with . /// /// The name of the parameter that caused the current exception. /// The value of the argument that causes this exception. /// The logged . internal static ArgumentException ArgumentUriHasQueryOrFragment(string parameterName, Uri actualValue) { return new ArgumentException(Error.Format(CommonWebApiResources.ArgumentUriHasQueryOrFragment, actualValue), parameterName); } /// /// Creates an with the provided properties. /// /// The logged . [SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly", Justification = "The purpose of this API is to return an error for properties")] internal static ArgumentNullException PropertyNull() { return new ArgumentNullException("value"); } /// /// Creates an with the provided properties. /// /// The name of the parameter that caused the current exception. /// The logged . internal static ArgumentNullException ArgumentNull(string parameterName) { return new ArgumentNullException(parameterName); } /// /// Creates an with the provided properties. /// /// The name of the parameter that caused the current exception. /// A composite format string explaining the reason for the exception. /// An object array that contains zero or more objects to format. /// The logged . internal static ArgumentNullException ArgumentNull(string parameterName, string messageFormat, params object[] messageArgs) { return new ArgumentNullException(parameterName, Error.Format(messageFormat, messageArgs)); } /// /// Creates an with a default message. /// /// The name of the parameter that caused the current exception. /// The logged . internal static ArgumentException ArgumentNullOrEmpty(string parameterName) { return Error.Argument(parameterName, CommonWebApiResources.ArgumentNullOrEmpty, parameterName); } /// /// Creates an with the provided properties. /// /// The name of the parameter that caused the current exception. /// The value of the argument that causes this exception. /// A composite format string explaining the reason for the exception. /// An object array that contains zero or more objects to format. /// The logged . internal static ArgumentOutOfRangeException ArgumentOutOfRange(string parameterName, object actualValue, string messageFormat, params object[] messageArgs) { return new ArgumentOutOfRangeException(parameterName, actualValue, Error.Format(messageFormat, messageArgs)); } /// /// Creates an with a message saying that the argument must be greater than or equal to . /// /// The name of the parameter that caused the current exception. /// The value of the argument that causes this exception. /// The minimum size of the argument. /// The logged . internal static ArgumentOutOfRangeException ArgumentGreaterThanOrEqualTo(string parameterName, object actualValue, object minValue) { return new ArgumentOutOfRangeException(parameterName, actualValue, Error.Format(CommonWebApiResources.ArgumentMustBeGreaterThanOrEqualTo, minValue)); } /// /// Creates an with a message saying that the argument must be less than or equal to . /// /// The name of the parameter that caused the current exception. /// The value of the argument that causes this exception. /// The maximum size of the argument. /// The logged . internal static ArgumentOutOfRangeException ArgumentMustBeLessThanOrEqualTo(string parameterName, object actualValue, object maxValue) { return new ArgumentOutOfRangeException(parameterName, actualValue, Error.Format(CommonWebApiResources.ArgumentMustBeLessThanOrEqualTo, maxValue)); } /// /// Creates an with a message saying that the key was not found. /// /// The logged . internal static KeyNotFoundException KeyNotFound() { return new KeyNotFoundException(); } /// /// Creates an with a message saying that the key was not found. /// /// A composite format string explaining the reason for the exception. /// An object array that contains zero or more objects to format. /// The logged . internal static KeyNotFoundException KeyNotFound(string messageFormat, params object[] messageArgs) { return new KeyNotFoundException(Error.Format(messageFormat, messageArgs)); } /// /// Creates an initialized according to guidelines. /// /// A composite format string explaining the reason for the exception. /// An object array that contains zero or more objects to format. /// The logged . internal static ObjectDisposedException ObjectDisposed(string messageFormat, params object[] messageArgs) { // Pass in null, not disposedObject.GetType().FullName as per the above guideline return new ObjectDisposedException(null, Error.Format(messageFormat, messageArgs)); } /// /// Creates an initialized with the provided parameters. /// /// The logged . internal static OperationCanceledException OperationCanceled() { return new OperationCanceledException(); } /// /// Creates an initialized with the provided parameters. /// /// A composite format string explaining the reason for the exception. /// An object array that contains zero or more objects to format. /// The logged . internal static OperationCanceledException OperationCanceled(string messageFormat, params object[] messageArgs) { return new OperationCanceledException(Error.Format(messageFormat, messageArgs)); } /// /// Creates an . /// /// The name of the parameter that caused the current exception. /// The value of the argument that failed. /// A that represents the enumeration class with the valid values. /// The logged . internal static InvalidEnumArgumentException InvalidEnumArgument(string parameterName, int invalidValue, Type enumClass) { return new InvalidEnumArgumentException(parameterName, invalidValue, enumClass); } /// /// Creates an . /// /// A composite format string explaining the reason for the exception. /// An object array that contains zero or more objects to format. /// The logged . internal static InvalidOperationException InvalidOperation(string messageFormat, params object[] messageArgs) { return new InvalidOperationException(Error.Format(messageFormat, messageArgs)); } /// /// Creates an . /// /// Inner exception /// A composite format string explaining the reason for the exception. /// An object array that contains zero or more objects to format. /// The logged . internal static InvalidOperationException InvalidOperation(Exception innerException, string messageFormat, params object[] messageArgs) { return new InvalidOperationException(Error.Format(messageFormat, messageArgs), innerException); } /// /// Creates an . /// /// A composite format string explaining the reason for the exception. /// An object array that contains zero or more objects to format. /// The logged . internal static NotSupportedException NotSupported(string messageFormat, params object[] messageArgs) { return new NotSupportedException(Error.Format(messageFormat, messageArgs)); } } }