ServerPortfolio  2.0
Python parsers and server
 All Classes Namespaces Files Functions Variables Properties Pages
PortfolioException.py
Go to the documentation of this file.
1 ## @package serverportfolio.PortfolioException
2 #
3 # Define custom and specific exceptions for the complete package
4 #
5 # Last Changed $Id: PortfolioException.py 12 2015-04-09 08:26:44Z michael $
6 
7 
8 # needed if raise Error, None, sys.info[2]
9 import sys, traceback
10 
11 # doc python, base class is empty
12 # https://docs.python.org/2/reference/simple_stmts.html#grammar-token-raise_stmt
13 
14 # more evolved trick
15 # http://www.pydanny.com/attaching-custom-exceptions-to-functions-and-classes.html
16 
17 ## @class PortfolioError
18 # @brief Base class of the custom exceptions.
19 #
20 # Exception expects one argument *args (tuple) \n
21 # Can access with e.args, e.message works only with one argument.
23  # want to raise exception with a message formatted
24  # want to create an PortfolioError from a more defined one (like C++) ?
25 
26  ## @brief Constructor.
27  # @param message main string to report the error
28  # @param extra_message additional, will be append to message by a call to get_format_string
29  def __init__(self, message, extra_message = None) :
30  Exception.__init__(self, message)
31  #print "PortfolioError::init message= ", message
32  #print "extra_message ", extra_message
33 
34  self.extra_message = None
35  self.traceback = None
36 
37  if extra_message:
38  self.extra_message = message + extra_message
39 
40  #self.traceback = traceback.format_exc(3)
41  self.traceback = ''.join( traceback.format_tb( sys.exc_info()[2], 3 ) )
42  #print "self.traceback ", self.traceback
43 
44  def get_format_string(self):
45  #print "PortfolioError get_format_string() "
46  #string = 'PortfolioError: ' + self.message #+ '\n'
47  string = self.message + '\n'
48  if self.extra_message:
49  string += self.extra_message
50  if self.traceback:
51  string += "\n"+self.traceback
52  return string
53 
54  # method to overwrite to get nice printing
55  #def __str__(self):
56  # return repr(self.message)
57 
58 ## @class ParserError
59 # @brief Derived class specific to the parsers.\n
60 # Include specific information: stock, url, action
62 
63  # here fixe the number of arguments, using *args will get a tuple(StockOverflow)
64  # or using a dictionary... **kargs
65 
66  ## @brief Constructor
67  # @param message simple string to describe the error
68  # @param stock specify the stock symbols treated
69  # @param action EAction name performed
70  # @param the url used by WebQuery
71  def __init__(self, message, stock, action, url ):
72 
73  #print "ParserError::init message= ", message
74  # try to modify the message saved, cannot do this message not a string ?
75  # ok with str(message)
76  #message = 'original message ' + str(message)
77  message = str(message)
78  # extra argument easier for traceback
79  PortfolioError.__init__(self, message) # None, sys.exc_info()[2]
80 
81  # in principle should test if None here and in get_format_string
82  self.stock = stock
83  self.action = action
84  self.url = url
85 
86  #print "*** print_tb:"
87  #traceback.print_tb( tr_back, limit=4, file=sys.stdout)
88  #self.traceback2 = traceback.extract_tb( tr_back )
89 
90  # function call sys.exc_info(), moved to base class
91  # self.traceback = traceback.format_exc( 3 )
92  #print "self.traceback with ex\n", self.traceback
93  #print "End Constructor ParserError"
94 
95  ## @brief Produce more informative error message, including traceback
96  def get_format_string(self):
97 
98  string = 'ParserError: ' + self.message + '\n'
99  # problem with list of stocks
100  if self.stock:
101  string += 'stock: %s' % self.stock + '\n'
102  if self.action:
103  string += 'action: ' + self.action + '\n'
104  if self.url:
105  string += 'url: ' + self.url + '\n'
106  if self.traceback:
107  string += 'traceback:\n ' + self.traceback
108  return string
109 
110 # used ? tested ?
111 ## @class QueryError
112 # @brief Specific exception related to the web query (Utils.WebQuery)
114 
115  def __init__(self, message, url): #, e_action):
116  #print "QueryError::init"
117  #message = message + "\nurl :" + url #+ "\naction : " + e_action.name
118  message = str(message)
119  PortfolioError.__init__(self, message)
120 
121  # moved to base class, always available
122  #self.traceback = traceback.format_exc(3)
123  self.url = url
124 
125  # could write same, correclty called if a PortfolioError appear
126  def get_format_string(self):
127  #print "get_format_string QueryError"
128  #string = 'ParserError: ' + self.message + '\n'
129  string = self.message + '\n'
130  # problem with list of stocks
131  #if self.stock:
132  # string += 'stock: %s' % self.stock + '\n'
133  #if self.action:
134  # string += 'action: ' + self.action + '\n'
135  if self.url:
136  string += 'url: ' + self.url + '\n'
137  if self.traceback:
138  string += 'traceback:\n ' + self.traceback
139  return string
140 
141 # general FileError, XMLError
142 # class FileError
143 
144 ##########################################
145 if __name__ == "__main__":
146 
147  # see usage of
148  #print "exec_info ", sys.exc_info()
149  #print "exec_info[2] ", sys.exc_info()[2]
150  # with
151  # http://stackoverflow.com/questions/1350671/inner-exception-with-traceback-in-python
152  # http://stackoverflow.com/questions/4825234/exception-traceback-is-hidden-if-not-re-raised-immediately
153  print "Test PortfolioError 1 argument "
154  try:
155  raise PortfolioError( "A clear error message" )
156  except PortfolioError as e:
157  print "Got a PortfolioError"
158  print "e.args ", e.args
159  print "e ", e
160  print "e.message ", e.message # here works, defined in BaseException(deprecated??)
161 
162  print "\nTest PortfolioError 2 argument "
163  try:
164  raise PortfolioError( "A clear error message","An other" )
165  except PortfolioError as e:
166  print "Got a PortfolioError"
167  print "e.args ", e.args
168  print "e ", e
169  print "e.message ", e.message # here empty
170 
171  print "\n== Test ParserError"
172  try:
173  raise ParserError("An error from Parser", "An other error")
174  except ParserError, e:
175  print "\nGot a ParserError"
176  print "e ", e
177  print "e.args ", e.args
178  print "e.errors ", e.errors
179  print "e.message ", e.message
180 
181  print "\n== Test ParserError catched as PortfolioExcpetion"
182  try:
183  raise ParserError("An error from Parser", "An other error")
184  except PortfolioError, e:
185  print "\nGot a PortfolioError"
186  print "e ", e
187  print "e.args ", e.args
188  print "e.errors ", e.errors
189  print "e.message ", e.message
190 
Base class of the custom exceptions.
def get_format_string
Produce more informative error message, including traceback.
Specific exception related to the web query (Utils.WebQuery)
Derived class specific to the parsers.