Lean  $LEAN_TAG$
DllNotFoundPythonExceptionInterpreter.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 
19 {
20  /// <summary>
21  /// Interprets <see cref="DllNotFoundPythonExceptionInterpreter"/> instances
22  /// </summary>
24  {
25  /// <summary>
26  /// Determines the order that an instance of this class should be called
27  /// </summary>
28  public int Order => 0;
29 
30  /// <summary>
31  /// Determines if this interpreter should be applied to the specified exception.
32  /// </summary>
33  /// <param name="exception">The exception to check</param>
34  /// <returns>True if the exception can be interpreted, false otherwise</returns>
35  public bool CanInterpret(Exception exception)
36  {
37  return
38  exception?.GetType() == typeof(DllNotFoundException) &&
39  exception.Message.Contains("python");
40  }
41  /// <summary>
42  /// Interprets the specified exception into a new exception
43  /// </summary>
44  /// <param name="exception">The exception to be interpreted</param>
45  /// <param name="innerInterpreter">An interpreter that should be applied to the inner exception.</param>
46  /// <returns>The interpreted exception</returns>
47  public Exception Interpret(Exception exception, IExceptionInterpreter innerInterpreter)
48  {
49  var dnfe = (DllNotFoundException)exception;
50 
51  var startIndex = dnfe.Message.IndexOfInvariant("python");
52  var length = Math.Min(dnfe.Message.Length - startIndex, 10);
53  var dllName = dnfe.Message.Substring(startIndex, length);
54 
55  length = dllName.IndexOfInvariant('\'');
56  if (length > 0)
57  {
58  dllName = dllName.Substring(0, length);
59  }
60 
61  var platform = Environment.OSVersion.Platform.ToString();
62  var message = Messages.DllNotFoundPythonExceptionInterpreter.DynamicLinkLibraryNotFound(dllName, platform);
63  return new DllNotFoundException(message, dnfe);
64  }
65  }
66 }