|
| 1 | +/* The MIT License |
| 2 | + * |
| 3 | + * Copyright (c) 2013 Sam Harwell, Tunnel Vision Labs, LLC |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | + * a copy of this software and associated documentation files (the |
| 7 | + * "Software"), to deal in the Software without restriction, including |
| 8 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | + * permit persons to whom the Software is furnished to do so, subject to |
| 11 | + * the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace GitScc |
| 26 | +{ |
| 27 | + using System; |
| 28 | + using System.Reflection; |
| 29 | + |
| 30 | + public static class ExceptionExtensions |
| 31 | + { |
| 32 | + /// <summary> |
| 33 | + /// Provides an <see cref="Action{Exception}"/> delegate wrapping the |
| 34 | + /// <c>InternalPreserveStackTrace</c> method in the Microsoft .NET Framework. |
| 35 | + /// </summary> |
| 36 | + /// <remarks> |
| 37 | + /// This initializer does not consider other frameworks (e.g. Mono), but |
| 38 | + /// since this is part of a Visual Studio extension we know that the |
| 39 | + /// Microsoft implementation is the one in use. |
| 40 | + /// </remarks> |
| 41 | + private static readonly Action<Exception> _internalPreserveStackTrace = |
| 42 | + (Action<Exception>)Delegate.CreateDelegate( |
| 43 | + typeof(Action<Exception>), |
| 44 | + typeof(Exception).GetMethod( |
| 45 | + "InternalPreserveStackTrace", |
| 46 | + BindingFlags.Instance | BindingFlags.NonPublic)); |
| 47 | + |
| 48 | +#pragma warning disable 618 // 'System.ExecutionEngineException' is obsolete |
| 49 | + /// <summary> |
| 50 | + /// Returns <c>true</c> if <paramref name="e"/> is considered a critical |
| 51 | + /// exception, i.e. an exception which is likely to corrupt the process state |
| 52 | + /// and unless <em>explicitly</em> handled should result in an application crash. |
| 53 | + /// </summary> |
| 54 | + /// <param name="e">The exception</param> |
| 55 | + /// <returns><c>true</c> if <paramref name="e"/> is a critical exception, |
| 56 | + /// otherwise <c>false</c>.</returns> |
| 57 | + /// <exception cref="ArgumentNullException">if <paramref name="e"/> in <c>null</c>.</exception> |
| 58 | + public static bool IsCritical(this Exception e) |
| 59 | + { |
| 60 | + if (e == null) |
| 61 | + throw new ArgumentNullException("e"); |
| 62 | + |
| 63 | + if (e is AccessViolationException |
| 64 | + || e is StackOverflowException |
| 65 | + || e is ExecutionEngineException |
| 66 | + || e is OutOfMemoryException |
| 67 | + || e is BadImageFormatException |
| 68 | + || e is AppDomainUnloadedException) |
| 69 | + { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + return false; |
| 74 | + } |
| 75 | +#pragma warning restore 618 |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// This method ensures that the stack trace for <paramref name="e"/> is preserved |
| 79 | + /// before a <c>rethrow</c> statement. |
| 80 | + /// </summary> |
| 81 | + /// <remarks> |
| 82 | + /// <para> |
| 83 | + /// In the Microsoft .NET Framework, if an exception is thrown, caught, and rethrown |
| 84 | + /// all within the same method, by default the stack trace for the rethrown exception |
| 85 | + /// will indicate that the exception was initially thrown at the location of the |
| 86 | + /// <c>rethrow</c> statement. This method instructs the framework to instead |
| 87 | + /// preserve the original stack trace when the exception is rethrown. |
| 88 | + /// </para> |
| 89 | + /// |
| 90 | + /// <para> |
| 91 | + /// This is the default behavior when an exception is caught and rethrown in a |
| 92 | + /// <em>different</em> method from which it was originally thrown. |
| 93 | + /// </para> |
| 94 | + /// </remarks> |
| 95 | + /// <param name="e">The exception</param> |
| 96 | + /// <exception cref="ArgumentNullException">if <paramref name="e"/> in <c>null</c>.</exception> |
| 97 | + public static void PreserveStackTrace(this Exception e) |
| 98 | + { |
| 99 | + if (e == null) |
| 100 | + throw new ArgumentNullException("e"); |
| 101 | + |
| 102 | + _internalPreserveStackTrace(e); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments