Added some tests for OpenIdRelyingParty.
[dotnetoauth.git] / src / DotNetOpenAuth / Messaging / ErrorUtilities.cs
blob2b7fd34c0b93780b5251a3ead7b5e5b08c44ba42
1 //-----------------------------------------------------------------------
2 // <copyright file="ErrorUtilities.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth.Messaging {
8 using System;
9 using System.Diagnostics;
10 using System.Globalization;
11 using System.Web;
13 /// <summary>
14 /// A collection of error checking and reporting methods.
15 /// </summary>
16 internal class ErrorUtilities {
17 /// <summary>
18 /// Wraps an exception in a new <see cref="ProtocolException"/>.
19 /// </summary>
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);
28 /// <summary>
29 /// Throws an internal error exception.
30 /// </summary>
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);
37 /// <summary>
38 /// Checks a condition and throws an internal error exception if it evaluates to false.
39 /// </summary>
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) {
44 if (!condition) {
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) {
49 Debugger.Break();
52 throw new InternalErrorException(errorMessage);
56 /// <summary>
57 /// Checks a condition and throws an internal error exception if it evaluates to false.
58 /// </summary>
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) {
64 if (!condition) {
65 errorMessage = string.Format(CultureInfo.CurrentCulture, errorMessage, args);
66 throw new InternalErrorException(errorMessage);
70 /// <summary>
71 /// Checks a condition and throws an <see cref="InvalidOperationException"/> if it evaluates to false.
72 /// </summary>
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) {
77 if (!condition) {
78 throw new InvalidOperationException(errorMessage);
82 /// <summary>
83 /// Checks a condition and throws an <see cref="InvalidOperationException"/> if it evaluates to false.
84 /// </summary>
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) {
90 if (!condition) {
91 errorMessage = string.Format(CultureInfo.CurrentCulture, errorMessage, args);
92 throw new InvalidOperationException(errorMessage);
96 /// <summary>
97 /// Throws a <see cref="ProtocolException"/> if some <paramref name="condition"/> evaluates to false.
98 /// </summary>
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) {
105 if (!condition) {
106 throw new ProtocolException(string.Format(CultureInfo.CurrentCulture, errorMessage, args), faultedMessage);
110 /// <summary>
111 /// Throws a <see cref="ProtocolException"/> if some <paramref name="condition"/> evaluates to false.
112 /// </summary>
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) {
118 if (!condition) {
119 throw new ProtocolException(string.Format(
120 CultureInfo.CurrentCulture,
121 message,
122 args));
126 /// <summary>
127 /// Throws a <see cref="ProtocolException"/>.
128 /// </summary>
129 /// <param name="message">The message to set in the exception.</param>
130 /// <param name="args">The formatting arguments of the message.</param>
131 /// <returns>
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.
135 /// </returns>
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();
144 /// <summary>
145 /// Verifies something about the argument supplied to a method.
146 /// </summary>
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) {
152 if (!condition) {
153 throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, message, args));
157 /// <summary>
158 /// Verifies something about the argument supplied to a method.
159 /// </summary>
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) {
164 if (!condition) {
165 throw new ArgumentOutOfRangeException(parameterName);
169 /// <summary>
170 /// Verifies something about the argument supplied to a method.
171 /// </summary>
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) {
178 if (!condition) {
179 throw new ArgumentOutOfRangeException(parameterName, string.Format(message, args));
183 /// <summary>
184 /// Verifies something about the argument supplied to a method.
185 /// </summary>
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) {
192 if (!condition) {
193 throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, message, args), parameterName);
197 /// <summary>
198 /// Verifies that some given value is not null.
199 /// </summary>
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);
209 /// <summary>
210 /// Verifies that some string is not null and has non-zero length.
211 /// </summary>
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);
223 /// <summary>
224 /// Verifies that <see cref="HttpContext.Current"/> != <c>null</c>.
225 /// </summary>
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);