Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Runtime / ExceptionHelpers.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
6  * copy of the license can be found in the License.html file at the root of this distribution. If 
7  * you cannot locate the  Apache License, Version 2.0, please send an email to 
8  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
9  * by the terms of the Apache License, Version 2.0.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15 #if FEATURE_CORE_DLR
16 using System.Linq.Expressions;
17 #else
18 using Microsoft.Scripting.Ast;
19 #endif
20
21 using System;
22 using System.Collections.Generic;
23 using System.Diagnostics;
24 using System.Reflection;
25 using System.Threading;
26 using Microsoft.Scripting.Actions;
27 using Microsoft.Scripting.Generation;
28
29 namespace Microsoft.Scripting.Runtime {
30     public static class ExceptionHelpers {
31 #if FEATURE_STACK_TRACE
32         private const string prevStackTraces = "PreviousStackTraces";
33
34         /// <summary>
35         /// Updates an exception before it's getting re-thrown so
36         /// we can present a reasonable stack trace to the user.
37         /// </summary>
38         public static Exception UpdateForRethrow(Exception rethrow) {
39 #if !SILVERLIGHT
40             List<StackTrace> prev;
41
42             // we don't have any dynamic stack trace data, capture the data we can
43             // from the raw exception object.
44             StackTrace st = new StackTrace(rethrow, true);
45
46             if (!TryGetAssociatedStackTraces(rethrow, out prev)) {
47                 prev = new List<StackTrace>();
48                 AssociateStackTraces(rethrow, prev);
49             }
50
51             prev.Add(st);
52
53 #endif
54             return rethrow;
55         }
56
57         /// <summary>
58         /// Returns all the stack traces associates with an exception
59         /// </summary>
60         public static IList<StackTrace> GetExceptionStackTraces(Exception rethrow) {
61             List<StackTrace> result;
62             return TryGetAssociatedStackTraces(rethrow, out result) ? result : null;
63         }
64
65         private static void AssociateStackTraces(Exception e, List<StackTrace> traces) {
66             e.Data[prevStackTraces] = traces;
67         }
68
69         private static bool TryGetAssociatedStackTraces(Exception e, out List<StackTrace> traces) {
70             traces = e.Data[prevStackTraces] as List<StackTrace>;
71             return traces != null;
72         }        
73 #else
74         public static Exception UpdateForRethrow(Exception rethrow) {
75             return rethrow;
76         }
77 #endif
78     }
79 }