1
//-----------------------------------------------------------------------
2 // <copyright file="ErrorUtilities.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.Messaging
{
9 using System
.Diagnostics
;
10 using System
.Globalization
;
14 /// A collection of error checking and reporting methods.
16 internal class ErrorUtilities
{
18 /// Wraps an exception in a new <see cref="ProtocolException"/>.
20 /// <param name="inner">The inner exception to wrap.</param>
21 /// <param name="errorMessage">The error message for the outer exception.</param>
22 /// <param name="args">The string formatting arguments, if any.</param>
23 /// <returns>The newly constructed (unthrown) exception.</returns>
24 internal static Exception
Wrap(Exception inner
, string errorMessage
, params object[] args
) {
25 return new ProtocolException(string.Format(CultureInfo
.CurrentCulture
, errorMessage
, args
), inner
);
29 /// Throws an internal error exception.
31 /// <param name="errorMessage">The error message.</param>
32 /// <exception cref="InternalErrorException">Always thrown.</exception>
33 internal static void ThrowInternal(string errorMessage
) {
34 VerifyInternal(false, errorMessage
);
38 /// Checks a condition and throws an internal error exception if it evaluates to false.
40 /// <param name="condition">The condition to check.</param>
41 /// <param name="errorMessage">The message to include in the exception, if created.</param>
42 /// <exception cref="InternalErrorException">Thrown if <paramref name="condition"/> evaluates to <c>false</c>.</exception>
43 internal static void VerifyInternal(bool condition
, string errorMessage
) {
45 // Since internal errors are really bad, take this chance to
46 // help the developer find the cause by breaking into the
47 // debugger if one is attached.
48 if (Debugger
.IsAttached
) {
52 throw new InternalErrorException(errorMessage
);
57 /// Checks a condition and throws an internal error exception if it evaluates to false.
59 /// <param name="condition">The condition to check.</param>
60 /// <param name="errorMessage">The message to include in the exception, if created.</param>
61 /// <param name="args">The formatting arguments.</param>
62 /// <exception cref="InternalErrorException">Thrown if <paramref name="condition"/> evaluates to <c>false</c>.</exception>
63 internal static void VerifyInternal(bool condition
, string errorMessage
, params object[] args
) {
65 errorMessage
= string.Format(CultureInfo
.CurrentCulture
, errorMessage
, args
);
66 throw new InternalErrorException(errorMessage
);
71 /// Checks a condition and throws an <see cref="InvalidOperationException"/> if it evaluates to false.
73 /// <param name="condition">The condition to check.</param>
74 /// <param name="errorMessage">The message to include in the exception, if created.</param>
75 /// <exception cref="InvalidOperationException">Thrown if <paramref name="condition"/> evaluates to <c>false</c>.</exception>
76 internal static void VerifyOperation(bool condition
, string errorMessage
) {
78 throw new InvalidOperationException(errorMessage
);
83 /// Checks a condition and throws an <see cref="InvalidOperationException"/> if it evaluates to false.
85 /// <param name="condition">The condition to check.</param>
86 /// <param name="errorMessage">The message to include in the exception, if created.</param>
87 /// <param name="args">The formatting arguments.</param>
88 /// <exception cref="InvalidOperationException">Thrown if <paramref name="condition"/> evaluates to <c>false</c>.</exception>
89 internal static void VerifyOperation(bool condition
, string errorMessage
, params object[] args
) {
91 errorMessage
= string.Format(CultureInfo
.CurrentCulture
, errorMessage
, args
);
92 throw new InvalidOperationException(errorMessage
);
97 /// Throws a <see cref="ProtocolException"/> if some <paramref name="condition"/> evaluates to false.
99 /// <param name="condition">True to do nothing; false to throw the exception.</param>
100 /// <param name="faultedMessage">The message being processed that would be responsible for the exception if thrown.</param>
101 /// <param name="errorMessage">The error message for the exception.</param>
102 /// <param name="args">The string formatting arguments, if any.</param>
103 /// <exception cref="ProtocolException">Thrown if <paramref name="condition"/> evaluates to <c>false</c>.</exception>
104 internal static void VerifyProtocol(bool condition
, IProtocolMessage faultedMessage
, string errorMessage
, params object[] args
) {
106 throw new ProtocolException(string.Format(CultureInfo
.CurrentCulture
, errorMessage
, args
), faultedMessage
);
111 /// Throws a <see cref="ProtocolException"/> if some <paramref name="condition"/> evaluates to false.
113 /// <param name="condition">True to do nothing; false to throw the exception.</param>
114 /// <param name="message">The error message for the exception.</param>
115 /// <param name="args">The string formatting arguments, if any.</param>
116 /// <exception cref="ProtocolException">Thrown if <paramref name="condition"/> evaluates to <c>false</c>.</exception>
117 internal static void VerifyProtocol(bool condition
, string message
, params object[] args
) {
119 throw new ProtocolException(string.Format(
120 CultureInfo
.CurrentCulture
,
127 /// Throws a <see cref="ProtocolException"/>.
129 /// <param name="message">The message to set in the exception.</param>
130 /// <param name="args">The formatting arguments of the message.</param>
132 /// An InternalErrorException, which may be "thrown" by the caller in order
133 /// to satisfy C# rules to show that code will never be reached, but no value
134 /// actually is ever returned because this method guarantees to throw.
136 /// <exception cref="ProtocolException">Always thrown.</exception>
137 internal static Exception
ThrowProtocol(string message
, params object[] args
) {
138 VerifyProtocol(false, message
, args
);
140 // we never reach here, but this allows callers to "throw" this method.
141 return new InternalErrorException();
145 /// Verifies something about the argument supplied to a method.
147 /// <param name="condition">The condition that must evaluate to true to avoid an exception.</param>
148 /// <param name="message">The message to use in the exception if the condition is false.</param>
149 /// <param name="args">The string formatting arguments, if any.</param>
150 /// <exception cref="ArgumentException">Thrown if <paramref name="condition"/> evaluates to <c>false</c>.</exception>
151 internal static void VerifyArgument(bool condition
, string message
, params object[] args
) {
153 throw new ArgumentException(string.Format(CultureInfo
.CurrentCulture
, message
, args
));
158 /// Verifies something about the argument supplied to a method.
160 /// <param name="condition">The condition that must evaluate to true to avoid an exception.</param>
161 /// <param name="parameterName">Name of the parameter.</param>
162 /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="condition"/> evaluates to <c>false</c>.</exception>
163 internal static void VerifyArgumentInRange(bool condition
, string parameterName
) {
165 throw new ArgumentOutOfRangeException(parameterName
);
170 /// Verifies something about the argument supplied to a method.
172 /// <param name="condition">The condition that must evaluate to true to avoid an exception.</param>
173 /// <param name="parameterName">Name of the parameter.</param>
174 /// <param name="message">The unformatted message.</param>
175 /// <param name="args">The string formatting arguments.</param>
176 /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="condition"/> evaluates to <c>false</c>.</exception>
177 internal static void VerifyArgumentInRange(bool condition
, string parameterName
, string message
, params object[] args
) {
179 throw new ArgumentOutOfRangeException(parameterName
, string.Format(message
, args
));
184 /// Verifies something about the argument supplied to a method.
186 /// <param name="condition">The condition that must evaluate to true to avoid an exception.</param>
187 /// <param name="parameterName">Name of the parameter.</param>
188 /// <param name="message">The message to use in the exception if the condition is false.</param>
189 /// <param name="args">The string formatting arguments, if any.</param>
190 /// <exception cref="ArgumentException">Thrown if <paramref name="condition"/> evaluates to <c>false</c>.</exception>
191 internal static void VerifyArgumentNamed(bool condition
, string parameterName
, string message
, params object[] args
) {
193 throw new ArgumentException(string.Format(CultureInfo
.CurrentCulture
, message
, args
), parameterName
);
198 /// Verifies that some given value is not null.
200 /// <param name="value">The value to check.</param>
201 /// <param name="paramName">Name of the parameter, which will be used in the <see cref="ArgumentException"/>, if thrown.</param>
202 /// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is null.</exception>
203 internal static void VerifyArgumentNotNull(object value, string paramName
) {
204 if (Object
.ReferenceEquals(value, null)) {
205 throw new ArgumentNullException(paramName
);
210 /// Verifies that some string is not null and has non-zero length.
212 /// <param name="value">The value to check.</param>
213 /// <param name="paramName">Name of the parameter, which will be used in the <see cref="ArgumentException"/>, if thrown.</param>
214 /// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is null.</exception>
215 /// <exception cref="ArgumentException">Thrown if <paramref name="value"/> has zero length.</exception>
216 internal static void VerifyNonZeroLength(string value, string paramName
) {
217 VerifyArgumentNotNull(value, paramName
);
218 if (value.Length
== 0) {
219 throw new ArgumentException(MessagingStrings
.UnexpectedEmptyString
, paramName
);
224 /// Verifies that <see cref="HttpContext.Current"/> != <c>null</c>.
226 /// <exception cref="InvalidOperationException">Thrown if <see cref="HttpContext.Current"/> == <c>null</c></exception>
227 internal static void VerifyHttpContext() {
228 ErrorUtilities
.VerifyOperation(HttpContext
.Current
!= null, MessagingStrings
.HttpContextRequired
);