File: ExceptionHelpers.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (256 lines) | stat: -rw-r--r-- 10,794 bytes parent folder | download | duplicates (2)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Data.Entity
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Threading;

    /// <summary>
    ///     Common exception utilities.
    /// </summary>
    public static class ExceptionHelpers
    {
        /// <summary>
        ///     Gets the error message thrown for types which implement IEnumerable only for cleaner construction.
        /// </summary>
        public static string EnumerableNotImplementedExceptionMessage
        {
            get { return "IEnumerable is only implemented for cleaner construction."; }
        }

        /// <summary>
        ///     Throws ArgumentNullException if specified argument is null.
        /// </summary>
        /// <param name="argument"> Argument to check for null. </param>
        /// <param name="argumentName"> Argument name. </param>
        public static void CheckArgumentNotNull(object argument, string argumentName)
        {
            if (argument == null)
            {
                throw new ArgumentNullException(argumentName);
            }
        }

        /// <summary>
        ///     Throws ArgumentException if the range is invalid.
        /// </summary>
        /// <typeparam name="TValue"> The type of the value. Must be comparable. </typeparam>
        /// <param name="leftValue"> The left value. </param>
        /// <param name="leftParameterName"> Name of the left parameter. </param>
        /// <param name="rightValue"> The right value. </param>
        /// <param name="rightParameterName"> Name of the right parameter. </param>
        public static void CheckValidRange<TValue>(TValue leftValue, string leftParameterName, TValue rightValue, string rightParameterName)
            where TValue : struct, IComparable<TValue>
        {
            if (leftValue.CompareTo(rightValue) > 0)
            {
                throw new ArgumentException(
                    String.Format(
                        CultureInfo.InvariantCulture, "Invalid range specified - '{0}' must be greater than or equal to '{1}'.",
                        rightParameterName, leftParameterName));
            }
        }

        /// <summary>
        ///     Throws ArgumentException if string argument is empty and ArgumentNullException if string argument is null.
        /// </summary>
        /// <param name="argument"> String argument for check. </param>
        /// <param name="argumentName"> Argument name. </param>
        public static void CheckStringArgumentIsNotNullOrEmpty(string argument, string argumentName)
        {
            CheckArgumentNotNull(argument, argumentName);

            if (String.IsNullOrEmpty(argument))
            {
                var message = String.Format(CultureInfo.InvariantCulture, "Argument '{0}' cannot be empty.", argumentName);
                throw new ArgumentException(message);
            }
        }

        /// <summary>
        ///     Throws InvalidOperationException if specified string is null or empty
        /// </summary>
        /// <param name="value"> The string to check for null/empty </param>
        /// <param name="exceptionMessageFormatText"> The exception message. </param>
        /// <param name="messageArguments"> The format arguments (if any) for the exception message. </param>
        public static void CheckStringNotNullOrEmpty(string value, string exceptionMessageFormatText, params object[] messageArguments)
        {
            Assert(exceptionMessageFormatText != null, "message cannnot be null");
            Assert(messageArguments != null, "messageArguments cannnot be null");

            if (String.IsNullOrEmpty(value))
            {
                throw new InvalidOperationException(
                    String.Format(CultureInfo.InvariantCulture, exceptionMessageFormatText, messageArguments));
            }
        }

        /// <summary>
        ///     Throws InvalidOperationException if specified object is null.
        /// </summary>
        /// <param name="value"> The object to check for null. </param>
        /// <param name="exceptionMessageFormatText"> The exception message. </param>
        /// <param name="messageArguments"> The format arguments (if any) for the exception message. </param>
        public static void CheckObjectNotNull(object value, string exceptionMessageFormatText, params object[] messageArguments)
        {
            Assert(exceptionMessageFormatText != null, "message cannnot be null");
            Assert(messageArguments != null, "messageArguments cannnot be null");

            if (value == null)
            {
                var message = exceptionMessageFormatText;
                if (messageArguments.Length > 0)
                {
                    message = String.Format(CultureInfo.InvariantCulture, exceptionMessageFormatText, messageArguments);
                }

                throw new InvalidOperationException(message);
            }
        }

        /// <summary>
        ///     Throws ArgumentException if the given collection is null or empty.
        /// </summary>
        /// <typeparam name="TElement"> Type of the element type. </typeparam>
        /// <param name="argument"> The argument. </param>
        /// <param name="argumentName"> Name of the argument. </param>
        public static void CheckCollectionNotEmpty<TElement>(IEnumerable<TElement> argument, string argumentName)
        {
            CheckArgumentNotNull(argument, argumentName);

            if (!argument.Any())
            {
                throw new ArgumentException(
                    String.Format(CultureInfo.InvariantCulture, "Collection argument '{0}' must have at least one element.", argumentName));
            }
        }

        /// <summary>
        ///     Throws ArgumentException if the given collection is null or contains null elements.
        /// </summary>
        /// <typeparam name="TElement"> Type of the element type. </typeparam>
        /// <param name="argument"> The argument. </param>
        /// <param name="argumentName"> Name of the argument. </param>
        public static void CheckCollectionDoesNotContainNulls<TElement>(IEnumerable<TElement> argument, string argumentName)
        {
            CheckArgumentNotNull(argument, argumentName);

            if (argument.Any(e => e == null))
            {
                throw new ArgumentException(
                    String.Format(CultureInfo.InvariantCulture, "Collection argument '{0}' cannot contain null elements.", argumentName));
            }
        }

        /// <summary>
        ///     Throws NotSupportedException saying that IEnumerable is only implemented for cleaner construction.
        /// </summary>
        /// <returns> NotSupportedException with appropriate message </returns>
        public static NotSupportedException CreateIEnumerableNotImplementedException()
        {
            return new NotSupportedException(EnumerableNotImplementedExceptionMessage);
        }

        /// <summary>
        ///     Determines whether the specified exception is catchable.
        /// </summary>
        /// <param name="exception"> The exception. </param>
        /// <returns> A value <c>true</c> if the specified exception is catchable; otherwise, <c>false</c> . </returns>
        public static bool IsCatchable(Exception exception)
        {
            if (exception is ThreadAbortException)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        ///     Executes the given delegate and returns the exception that it throws.
        /// </summary>
        public static Exception GenerateException(Action willThrow)
        {
            try
            {
                willThrow();
            }
            catch (Exception ex)
            {
                return ex;
            }
            Debug.Fail("Expected willThrow delegate to throw.");
            return null;
        }

        /// <summary>
        ///     Serializes and de-serializes the given exception and returns the de-serialized instance.
        /// </summary>
        public static TException SerializeAndDeserialize<TException>(TException exception) where TException : Exception
        {
            var stream = new MemoryStream();
            var formatter = new BinaryFormatter();

            formatter.Serialize(stream, exception);
            stream.Seek(0, SeekOrigin.Begin);

            return (TException)formatter.Deserialize(stream);
        }

        /// <summary>
        ///     Asserts the specified condition to be true and throws exception if it is not.
        /// </summary>
        /// <param name="condition"> If set to <c>true</c> , the exception will not be thrown. </param>
        /// <param name="errorMessage"> The error message. </param>
        /// <param name="messageArguments"> Arguments for the error message. </param>
        public static void Assert(bool condition, string errorMessage, params object[] messageArguments)
        {
            if (!condition)
            {
                throw new InvalidOperationException(
                    "ASSERTION FAILED: " + String.Format(CultureInfo.InvariantCulture, errorMessage, messageArguments));
            }
        }

        public static void UnwrapAggregateExceptions(Action executor)
        {
            UnwrapAggregateExceptions<object>(
                () =>
                {
                    executor();
                    return null;
                });
        }

        /// <summary>
        ///     Examines the AggregateExceptions thrown by the <paramref name="executor" />
        ///     and rethrows the inner exception if only one is contained.
        /// </summary>
        public static T UnwrapAggregateExceptions<T>(Func<T> executor)
        {
            T result;
            try
            {
                result = executor();
            }
            catch (AggregateException ae)
            {
                if (ae.InnerExceptions.Count == 1)
                {
                    throw ae.InnerExceptions.Single();
                }
                else
                {
                    throw;
                }
            }

            return result;
        }
    }
}