summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core/Assumes.cs
blob: 67205a2fd2085edebc90e073bac591d2dd09db68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//-----------------------------------------------------------------------
// <copyright file="Assumes.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth {
	using System;
	using System.Collections.Generic;
	using System.Diagnostics;
	using System.Diagnostics.Contracts;
	using System.Linq;
	using System.Text;

	/// <summary>
	/// Internal state consistency checks that throw an internal error exception when they fail.
	/// </summary>
	internal static class Assumes {
		/// <summary>
		/// Validates some expression describing the acceptable condition evaluates to true.
		/// </summary>
		/// <param name="condition">The expression that must evaluate to true to avoid an internal error exception.</param>
		/// <param name="message">The message to include with the exception.</param>
		[Pure, DebuggerStepThrough]
		internal static void True(bool condition, string message = null) {
			if (!condition) {
				Fail(message);
			}
		}

		/// <summary>
		/// Validates some expression describing the acceptable condition evaluates to true.
		/// </summary>
		/// <param name="condition">The expression that must evaluate to true to avoid an internal error exception.</param>
		/// <param name="unformattedMessage">The unformatted message.</param>
		/// <param name="args">Formatting arguments.</param>
		[Pure, DebuggerStepThrough]
		internal static void True(bool condition, string unformattedMessage, params object[] args) {
			if (!condition) {
				Fail(String.Format(unformattedMessage, args));
			}
		}

		/// <summary>
		/// Throws an internal error exception.
		/// </summary>
		/// <param name="message">The message.</param>
		[Pure, DebuggerStepThrough]
		internal static void Fail(string message = null) {
			if (message != null) {
				throw new InternalErrorException(message);
			} else {
				throw new InternalErrorException();
			}
		}

		/// <summary>
		/// An internal error exception that should never be caught.
		/// </summary>
		[Serializable]
		private class InternalErrorException : Exception {
			/// <summary>
			/// Initializes a new instance of the <see cref="InternalErrorException"/> class.
			/// </summary>
			internal InternalErrorException() {
			}

			/// <summary>
			/// Initializes a new instance of the <see cref="InternalErrorException"/> class.
			/// </summary>
			/// <param name="message">The message.</param>
			internal InternalErrorException(string message) : base(message) {
			}

			/// <summary>
			/// Initializes a new instance of the <see cref="InternalErrorException"/> class.
			/// </summary>
			/// <param name="message">The message.</param>
			/// <param name="inner">The inner exception.</param>
			internal InternalErrorException(string message, Exception inner) : base(message, inner) {
			}

			/// <summary>
			/// Initializes a new instance of the <see cref="InternalErrorException"/> class.
			/// </summary>
			/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
			/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
			/// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception>
			/// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
			protected InternalErrorException(
			  System.Runtime.Serialization.SerializationInfo info,
			  System.Runtime.Serialization.StreamingContext context)
				: base(info, context) {
			}
		}
	}
}