-
Notifications
You must be signed in to change notification settings - Fork 9
/
Throw.State.cs
164 lines (151 loc) · 6.81 KB
/
Throw.State.cs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace CK.Core;
public partial class Throw
{
/// <summary>
/// Throws a new <see cref="InvalidOperationException"/> if <paramref name="valid"/> expression is false.
/// </summary>
/// <param name="valid">The expression to that must be true.</param>
/// <param name="exp">Roslyn's automatic capture of the expression's value.</param>
[StackTraceHidden]
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static void CheckState( [DoesNotReturnIf( false )] bool valid, [CallerArgumentExpression( "valid" )] string? exp = null )
{
if( !valid )
{
CheckStateException( exp! );
}
}
/// <summary>
/// Throws a new <see cref="InvalidOperationException"/> if <paramref name="valid"/> expression is false.
/// </summary>
/// <param name="message">An explicit message that replaces the default "Invalid state: ... should be true.".</param>
/// <param name="valid">The expression to that must be true.</param>
/// <param name="exp">Roslyn's automatic capture of the expression's value.</param>
[StackTraceHidden]
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static void CheckState( string message, [DoesNotReturnIf( false )] bool valid, [CallerArgumentExpression( "valid" )] string? exp = null )
{
if( !valid )
{
CheckStateException( exp!, message );
}
}
[DoesNotReturn]
[MethodImpl( MethodImplOptions.NoInlining )]
static void CheckStateException( string exp, string? message = null )
{
if( message == null )
{
InvalidOperationException( $"Invalid state: '{exp}' should be true." );
}
else
{
InvalidOperationException( $"{message} (Expression: '{exp}')" );
}
}
/// <summary>
/// Throws a new <see cref="System.InvalidOperationException"/>.
/// </summary>
/// <param name="message">Optional message to include in the exception.</param>
/// <param name="innerException">Optional inner <see cref="Exception"/> to include.</param>
[StackTraceHidden]
[DoesNotReturn]
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static void InvalidOperationException( string? message = null, Exception? innerException = null )
{
InvalidOperationException<object>( message, innerException );
}
/// <summary>
/// Throws a new <see cref="System.InvalidOperationException"/> but formally returns a <typeparamref name="T"/> value.
/// Can be used in switch expressions or as a returned value.
/// </summary>
/// <param name="message">Optional message to include in the exception.</param>
/// <param name="innerException">Optional inner <see cref="Exception"/> to include.</param>
[StackTraceHidden]
[DoesNotReturn]
[MethodImpl( MethodImplOptions.NoInlining )]
public static T InvalidOperationException<T>( string? message = null, Exception? innerException = null )
{
throw new InvalidOperationException( message, innerException );
}
/// <summary>
/// Throws a new <see cref="System.ObjectDisposedException"/>.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">Optional inner <see cref="Exception"/> to include.</param>
[StackTraceHidden]
[DoesNotReturn]
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static void ObjectDisposedException( string? message, Exception? innerException )
{
ObjectDisposedException<object>( message, innerException );
}
/// <summary>
/// Throws a new <see cref="System.ObjectDisposedException"/> but formally returns a <typeparamref name="T"/> value.
/// Can be used in switch expressions or as a returned value.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">Optional inner <see cref="Exception"/> to include.</param>
[StackTraceHidden]
[DoesNotReturn]
[MethodImpl( MethodImplOptions.NoInlining )]
public static T ObjectDisposedException<T>( string? message, Exception? innerException )
{
throw new ObjectDisposedException( message, innerException );
}
/// <summary>
/// Throws a new <see cref="System.ObjectDisposedException"/>.
/// </summary>
/// <param name="objectName">The name of the disposed object.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
[StackTraceHidden]
[DoesNotReturn]
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static void ObjectDisposedException( string? objectName = null, string? message = null )
{
ObjectDisposedException<object>( objectName, message );
}
/// <summary>
/// Throws a new <see cref="System.ObjectDisposedException"/> but formally returns a <typeparamref name="T"/> value.
/// Can be used in switch expressions or as a returned value.
/// </summary>
/// <param name="objectName">The name of the disposed object.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
[StackTraceHidden]
[DoesNotReturn]
[MethodImpl( MethodImplOptions.NoInlining )]
public static T ObjectDisposedException<T>( string? objectName = null, string? message = null )
{
if( message == null ) throw new ObjectDisposedException( objectName );
throw new ObjectDisposedException( objectName, message );
}
/// <summary>
/// Throws a new <see cref="System.TimeoutException"/>.
/// </summary>
/// <param name="message">Optional message to include in the exception.</param>
/// <param name="innerException">Optional inner <see cref="Exception"/> to include.</param>
[StackTraceHidden]
[DoesNotReturn]
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static void TimeoutException( string? message, Exception? innerException )
{
TimeoutException<object>( message, innerException );
}
/// <summary>
/// Throws a new <see cref="System.TimeoutException"/> but formally returns a <typeparamref name="T"/> value.
/// Can be used in switch expressions or as a returned value.
/// </summary>
/// <param name="message">Optional message to include in the exception.</param>
/// <param name="innerException">Optional inner <see cref="Exception"/> to include.</param>
[StackTraceHidden]
[DoesNotReturn]
[MethodImpl( MethodImplOptions.NoInlining )]
public static T TimeoutException<T>( string? message, Exception? innerException )
{
throw new TimeoutException( message, innerException );
}
}