Lean  $LEAN_TAG$
Messages.Exceptions.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.Runtime.CompilerServices;
18 using Python.Runtime;
20 
21 namespace QuantConnect
22 {
23  /// <summary>
24  /// Provides user-facing message construction methods and static messages for the <see cref="Exceptions"/> namespace
25  /// </summary>
26  public static partial class Messages
27  {
28  /// <summary>
29  /// Provides user-facing messages for the <see cref="Exceptions.DllNotFoundPythonExceptionInterpreter"/> class and its consumers or related classes
30  /// </summary>
32  {
33  [MethodImpl(MethodImplOptions.AggressiveInlining)]
34  public static string DynamicLinkLibraryNotFound(string dllName, string platform)
35  {
36  return $"The dynamic-link library for {dllName} could not be found. " +
37  "Please visit https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/readme.md for instructions " +
38  $"on how to enable python support in {platform}";
39  }
40  }
41 
42  /// <summary>
43  /// Provides user-facing messages for the <see cref="Exceptions.InvalidTokenPythonExceptionInterpreter"/> class and its consumers or related classes
44  /// </summary>
46  {
47  public static string InvalidTokenExpectedSubstring = "invalid token";
48 
49  public static string NotPermittedExpectedSubstring = "are not permitted;";
50 
51  [MethodImpl(MethodImplOptions.AggressiveInlining)]
52  public static string InterpretException(PythonException exception)
53  {
54  var message = "Trying to include an invalid token/character in any statement throws a SyntaxError exception. " +
55  "To prevent the exception, ensure no invalid token are mistakenly included (e.g: leading zero).";
56  var errorLine = exception.Message.GetStringBetweenChars('(', ')');
57 
58  return $"{message}{Environment.NewLine} in {errorLine}{Environment.NewLine}";
59  }
60  }
61 
62  /// <summary>
63  /// Provides user-facing messages for the <see cref="Exceptions.KeyErrorPythonExceptionInterpreter"/> class and its consumers or related classes
64  /// </summary>
66  {
67  [MethodImpl(MethodImplOptions.AggressiveInlining)]
68  public static string KeyNotFoundInCollection(string key)
69  {
70  return "Trying to retrieve an element from a collection using a key that does not exist " +
71  $@"in that collection throws a KeyError exception. To prevent the exception, ensure that the {
72  key} key exist in the collection and/or that collection is not empty.";
73  }
74  }
75 
76  /// <summary>
77  /// Provides user-facing messages for the <see cref="Exceptions.NoMethodMatchPythonExceptionInterpreter"/> class and its consumers or related classes
78  /// </summary>
80  {
81  public static string NoMethodMatchExpectedSubstring = "No method match";
82 
83  [MethodImpl(MethodImplOptions.AggressiveInlining)]
84  public static string AttemptedToAccessMethodThatDoesNotExist(string methodName)
85  {
86  return "Trying to dynamically access a method that does not exist throws a TypeError exception. " +
87  $@"To prevent the exception, ensure each parameter type matches those required by the {
88  methodName} method. Please checkout the API documentation.";
89  }
90  }
91 
92  /// <summary>
93  /// Provides user-facing messages for the <see cref="Exceptions.ScheduledEventExceptionInterpreter"/> class and its consumers or related classes
94  /// </summary>
96  {
97  [MethodImpl(MethodImplOptions.AggressiveInlining)]
98  public static string ScheduledEventName(string eventName)
99  {
100  return $"In Scheduled Event '{eventName}',";
101  }
102  }
103 
104  /// <summary>
105  /// Provides user-facing messages for the <see cref="Exceptions.StackExceptionInterpreter"/> class and its consumers or related classes
106  /// </summary>
107  public static class StackExceptionInterpreter
108  {
109  [MethodImpl(MethodImplOptions.AggressiveInlining)]
110  public static string LoadedExceptionInterpreter(IExceptionInterpreter interpreter)
111  {
112  return $"Loaded ExceptionInterpreter: {interpreter.GetType().Name}";
113  }
114  }
115 
116  /// <summary>
117  /// Provides user-facing messages for the <see cref="Exceptions.UnsupportedOperandPythonExceptionInterpreter"/> class and its consumers or related classes
118  /// </summary>
120  {
121  public static string UnsupportedOperandTypeExpectedSubstring = "unsupported operand type";
122 
123  [MethodImpl(MethodImplOptions.AggressiveInlining)]
124  public static string InvalidObjectTypesForOperation(string types)
125  {
126  return $@"Trying to perform a summation, subtraction, multiplication or division between {
127  types} objects throws a TypeError exception. To prevent the exception, ensure that both values share the same type.";
128  }
129  }
130  }
131 }