Lean  $LEAN_TAG$
SystemExceptionInterpreter.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Text.RegularExpressions;
18 
20 {
21  /// <summary>
22  /// Base handler that will try get an exception file and line
23  /// </summary>
25  {
26  private static Regex FileAndLineRegex = new ("(\\w+.cs:line \\d+)", RegexOptions.Compiled);
27 
28  /// <summary>
29  /// Determines the order that an instance of this class should be called
30  /// </summary>
31  public virtual int Order => int.MaxValue;
32 
33  /// <summary>
34  /// Determines if this interpreter should be applied to the specified exception. f
35  /// </summary>
36  /// <param name="exception">The exception to check</param>
37  /// <returns>True if the exception can be interpreted, false otherwise</returns>
38  public virtual bool CanInterpret(Exception exception) => true;
39 
40  /// <summary>
41  /// Interprets the specified exception into a new exception
42  /// </summary>
43  /// <param name="exception">The exception to be interpreted</param>
44  /// <param name="innerInterpreter">An interpreter that should be applied to the inner exception.</param>
45  /// <returns>The interpreted exception</returns>
46  public virtual Exception Interpret(Exception exception, IExceptionInterpreter innerInterpreter)
47  {
48  var sanitized = new SanitizedException(exception.Message, exception.StackTrace);
49 
50  if (!TryGetLineAndFile(exception.StackTrace, out var fileAndLine))
51  {
52  return sanitized;
53  }
54  return new Exception(exception.Message + fileAndLine, innerException: sanitized);
55  }
56 
57  /// <summary>
58  /// Helper method to get the file and line from a C# stacktrace
59  /// </summary>
60  public static bool TryGetLineAndFile(string stackTrace, out string fileAndLine)
61  {
62  fileAndLine = null;
63  if (stackTrace != null)
64  {
65  var match = FileAndLineRegex.Match(stackTrace);
66  if (match.Success)
67  {
68  foreach (Match lineCapture in match.Captures)
69  {
70  fileAndLine = $" in {lineCapture.Groups[1].Value}" ;
71  return true;
72  }
73  }
74  }
75  return false;
76  }
77 
78  private class SanitizedException : Exception
79  {
80  private readonly string _message;
81  private readonly string _stackTrace;
82 
83  public override string Message => _message;
84  public override string StackTrace => _stackTrace;
85 
86  public SanitizedException(string message, string stackTrace)
87  {
88  _message = message;
89  _stackTrace = Logging.Log.ClearLeanPaths(stackTrace);
90  }
91  }
92  }
93 }