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  /// <summary>
34  /// Returns a string message saying the given dynamic-link library could not be found
35  /// </summary>
36  [MethodImpl(MethodImplOptions.AggressiveInlining)]
37  public static string DynamicLinkLibraryNotFound(string dllName, string platform)
38  {
39  return $"The dynamic-link library for {dllName} could not be found. " +
40  "Please visit https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/readme.md for instructions " +
41  $"on how to enable python support in {platform}";
42  }
43  }
44 
45  /// <summary>
46  /// Provides user-facing messages for the <see cref="Exceptions.InvalidTokenPythonExceptionInterpreter"/> class and its consumers or related classes
47  /// </summary>
49  {
50  /// <summary>
51  /// String message saying: invalid token
52  /// </summary>
53  public static string InvalidTokenExpectedSubstring = "invalid token";
54 
55  /// <summary>
56  /// String message saying: are not permitted
57  /// </summary>
58  public static string NotPermittedExpectedSubstring = "are not permitted;";
59 
60  /// <summary>
61  /// Returns a string message saying: Tring to include an invalid token/character in any statement throws s SyntaxError
62  /// exception. It also contains an advice to prevent that exception
63  /// </summary>
64  [MethodImpl(MethodImplOptions.AggressiveInlining)]
65  public static string InterpretException(PythonException exception)
66  {
67  var message = "Trying to include an invalid token/character in any statement throws a SyntaxError exception. " +
68  "To prevent the exception, ensure no invalid token are mistakenly included (e.g: leading zero).";
69  var errorLine = exception.Message.GetStringBetweenChars('(', ')');
70 
71  return $"{message}{Environment.NewLine} in {errorLine}{Environment.NewLine}";
72  }
73  }
74 
75  /// <summary>
76  /// Provides user-facing messages for the <see cref="Exceptions.KeyErrorPythonExceptionInterpreter"/> class and its consumers or related classes
77  /// </summary>
79  {
80  /// <summary>
81  /// Returns a string message saying the given key does not exists in the collection and the exception that is thrown
82  /// in this case. It also advises the user on how to prevent this exception
83  /// </summary>
84  [MethodImpl(MethodImplOptions.AggressiveInlining)]
85  public static string KeyNotFoundInCollection(string key)
86  {
87  return "Trying to retrieve an element from a collection using a key that does not exist " +
88  $@"in that collection throws a KeyError exception. To prevent the exception, ensure that the {
89  key} key exist in the collection and/or that collection is not empty.";
90  }
91  }
92 
93  /// <summary>
94  /// Provides user-facing messages for the <see cref="Exceptions.NoMethodMatchPythonExceptionInterpreter"/> class and its consumers or related classes
95  /// </summary>
97  {
98  /// <summary>
99  /// String message saying: No method match
100  /// </summary>
101  public static string NoMethodMatchExpectedSubstring = "No method match";
102 
103  /// <summary>
104  /// Returns a string message saying the given method does not exists. It also contains the exception
105  /// thrown is this case and an advice on how to prevent it
106  /// </summary>
107  [MethodImpl(MethodImplOptions.AggressiveInlining)]
108  public static string AttemptedToAccessMethodThatDoesNotExist(string methodName)
109  {
110  return "Trying to dynamically access a method that does not exist throws a TypeError exception. " +
111  $@"To prevent the exception, ensure each parameter type matches those required by the {
112  methodName} method. Please checkout the API documentation.";
113  }
114  }
115 
116  /// <summary>
117  /// Provides user-facing messages for the <see cref="Exceptions.ScheduledEventExceptionInterpreter"/> class and its consumers or related classes
118  /// </summary>
120  {
121  /// <summary>
122  /// Returns a string message with the given event name
123  /// </summary>
124  [MethodImpl(MethodImplOptions.AggressiveInlining)]
125  public static string ScheduledEventName(string eventName)
126  {
127  return $"In Scheduled Event '{eventName}',";
128  }
129  }
130 
131  /// <summary>
132  /// Provides user-facing messages for the <see cref="Exceptions.StackExceptionInterpreter"/> class and its consumers or related classes
133  /// </summary>
134  public static class StackExceptionInterpreter
135  {
136  /// <summary>
137  /// Returns a message for a Loaded Exception Interpreter
138  /// </summary>
139  [MethodImpl(MethodImplOptions.AggressiveInlining)]
140  public static string LoadedExceptionInterpreter(IExceptionInterpreter interpreter)
141  {
142  return $"Loaded ExceptionInterpreter: {interpreter.GetType().Name}";
143  }
144  }
145 
146  /// <summary>
147  /// Provides user-facing messages for the <see cref="Exceptions.UnsupportedOperandPythonExceptionInterpreter"/> class and its consumers or related classes
148  /// </summary>
150  {
151  /// <summary>
152  /// Unsupported Operand Type Expected substring
153  /// </summary>
154  public static string UnsupportedOperandTypeExpectedSubstring = "unsupported operand type";
155 
156  /// <summary>
157  /// Returns a message for invalid object types for operation
158  /// </summary>
159  [MethodImpl(MethodImplOptions.AggressiveInlining)]
160  public static string InvalidObjectTypesForOperation(string types)
161  {
162  return $@"Trying to perform a summation, subtraction, multiplication or division between {
163  types} objects throws a TypeError exception. To prevent the exception, ensure that both values share the same type.";
164  }
165  }
166  }
167 }