Python LMF library
 All Classes Namespaces Files Functions Variables
error_handling.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """! @package utils
4 """
5 
6 class Error(Exception):
7  """! Base class for exceptions in this library.
8  """
9  def __init__(self, msg, excp=None):
10  """! @brief Constructor.
11  @param msg String to be reported to user.
12  @param excp Raised system exception if any: IOError, KeyboardInterrupt, SystemExit, IndexError, KeyError, AttributeError, TypeError, NameError, UnboundLocalError, ValueError.
13  @return An Error instance.
14  """
15  self.msg = msg
16  self.excp = excp
17  # Retrieve caller information
18  import inspect
19  self.frame_info = inspect.getframeinfo(inspect.currentframe().f_back)
20 
21  def __str__(self):
22  """! @brief Build the string to be displayed.
23  @return A Python string.
24  """
25  from utils.io import EOL
26  # Follow Python display error style:
27  # File "user/scenario.py", line 5, in <module>
28  # from startup import *
29  result = " File \"" + self.frame_info.filename + "\", line " + str(self.frame_info.lineno) + ", in " + str(self.frame_info.function) + EOL
30  result += " Error: " + self.msg
31  return result
32 
33  def handle(self):
34  """! @brief Define behavior to follow in case this error is caught: diplay error and exit program.
35  """
36  print self
37  # If a system error occured, report it
38  if self.excp is not None:
39  print self.excp
40  # We consider that the error is not recoverable
41  import sys
42  sys.exit(-1)
43 
45  """! Exception raised for errors in the input.
46  """
47  def __init__(self, msg, expr=None):
48  """! @brief Constructor.
49  @param msg Explanation of the error.
50  @param expr Input expression in which the error occurred.
51  @return An InputError instance.
52  """
53  self.msg = msg
54  self.expr = expr
55  # Retrieve caller information
56  import inspect
57  self.frame_info = inspect.getframeinfo(inspect.currentframe().f_back)
58 
59  def handle(self):
60  """! @brief Define behavior to follow in case this error is caught: display error and exit program.
61  """
62  print self
63  # If there is input expression, report it
64  if self.expr is not None:
65  print " Input:", self.expr
66  else:
67  print " Input:", self.frame_info.code_context[self.frame_info.index]
68  # We consider that the error is not recoverable
69  import sys
70  sys.exit(-1)
71 
73  """! Exception raised for errors in the output.
74  """
75  def __init__(self, msg, expr=None):
76  """! @brief Constructor.
77  @param msg Explanation of the error.
78  @param expr Output expression in which the error occurred.
79  @return An OutputError instance.
80  """
81  self.msg = msg
82  self.expr = expr
83  # Retrieve caller information
84  import inspect
85  self.frame_info = inspect.getframeinfo(inspect.currentframe().f_back)
86 
87  def handle(self):
88  """! @brief Define behavior to follow in case this error is caught: display error and exit program.
89  """
90  print self
91  # If there is output expression, report it
92  if self.expr is not None:
93  print " Output:", self.expr
94  else:
95  print " Output:", self.frame_info.code_context[self.frame_info.index]
96  # We consider that the error is not recoverable
97  import sys
98  sys.exit(-1)
99 
100 class Warning(UserWarning):
101  """! Base class for warnings in this library.
102  """
103  def __init__(self, msg):
104  """! @brief Constructor.
105  @param msg String to be reported to user.
106  @return A Warning instance.
107  """
108  self.msg = msg
109  # Retrieve caller information
110  import inspect
111  self.frame_info = inspect.getframeinfo(inspect.currentframe().f_back)
112 
113  def __str__(self):
114  """! @brief Build the string to be displayed.
115  @return A Python string.
116  """
117  from utils.io import EOL
118  # Follow Python display error style
119  # File "user/scenario.py", line 5, in <module>
120  # from startup import *
121  result = " File \"" + self.frame_info.filename + "\", line " + str(self.frame_info.lineno) + ", in " + str(self.frame_info.function) + EOL
122  result += " Warning: " + self.msg
123  return result
Base class for warnings in this library.
Exception raised for errors in the input.
def __str__
Build the string to be displayed.
def handle
Define behavior to follow in case this error is caught: display error and exit program.
Base class for exceptions in this library.
def handle
Define behavior to follow in case this error is caught: display error and exit program.
def __str__
Build the string to be displayed.
def handle
Define behavior to follow in case this error is caught: diplay error and exit program.
Exception raised for errors in the output.