botsense  3.2.0
RoadNarrows Client-Server Proxied Services Framework
BotSenseError.py
Go to the documentation of this file.
1 ###############################################################################
2 #
3 # Package: BotSense
4 #
5 # File: BotSenseError.py
6 #
7 
8 """
9 BotSense Error Classes and Supporting Routines
10 """
11 
12 ## \file
13 ## \package BotSense.BotSenseError
14 ##
15 ## $LastChangedDate: 2010-09-13 10:25:05 -0600 (Mon, 13 Sep 2010) $
16 ## $Rev: 581 $
17 ##
18 ## \brief BotSense Error Classes and Routines
19 ##
20 ## \sa
21 ## \htmlonly
22 ## <a href="../pydoc/BotSense.BotSenseError.html">PyDoc Generated Documentation</a>
23 ## \endhtmlonly
24 ##
25 ## \author Robin Knight (robin.knight@roadnarrows.com)
26 ##
27 ## \copyright
28 ## \h_copy 2010-2017. RoadNarrows LLC.\n
29 ## http://www.roadnarrows.com\n
30 ## All Rights Reserved
31 ##
32 
33 # Permission is hereby granted, without written agreement and without
34 # license or royalty fees, to use, copy, modify, and distribute this
35 # software and its documentation for any purpose, provided that
36 # (1) The above copyright notice and the following two paragraphs
37 # appear in all copies of the source code and (2) redistributions
38 # including binaries reproduces these notices in the supporting
39 # documentation. Substantial modifications to this software may be
40 # copyrighted by their authors and need not follow the licensing terms
41 # described here, provided that the new terms are clearly indicated in
42 # all files where they apply.
43 #
44 # IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
45 # OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
46 # PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
47 # DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
48 # EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
49 # THE POSSIBILITY OF SUCH DAMAGE.
50 #
51 # THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
52 # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
53 # FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
54 # "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
55 # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
56 #
57 ###############################################################################
58 
59 import BotSenseCore as bsCore
60 
61 #--
62 def StrError(ecode):
63  """ Get the error string describing the BotSense error code.
64 
65  The absolute value of the error code is taken prior retrieving the
66  string. An unknown or out-of-range error code will be mapped to
67  BS_ECODE_BADEC.
68 
69  Parameters:
70  ecode - BotSense error code.
71 
72  Return:
73  The appropriate error code string.
74  """
75  sErr = bsCore.bsStrError(ecode)
76  if not sErr:
77  sErr = 'Error'
78  return sErr
79 ##
80 
81 #--
82 def ChkType(obj, T):
83  """ Check of oject is an instance of type T.
84 
85  Raises a BotSenseError exception if false.
86 
87  Parameters:
88  obj - Object instance.
89  T - Required type.
90  """
91  if not isinstance(obj, T):
92  if hasattr(T, '__name__'):
93  name = T.__name__
94  elif hasattr(T, '__class__.__name__'):
95  name = T.__class__.__name__
96  else:
97  name = repr(T)
98  raise BotSenseError(bsCore.BS_ECODE_BAD_VAL,
99  "Object type is not the '%s' required type." % (name))
100 ##
101 
102 #--
103 def ChkReturnIsOk(rc, emsg=None):
104  """ Check if return is ok (BsOk).
105 
106  Raises a BotSenseError exception if false.
107 
108  Parameters:
109  rc - BotSense return code.
110  emsg - Optional error message string.
111  """
112  if rc != bsCore.BS_OK:
113  raise BotSenseError(rc, emsg)
114 ##
115 
116 #--
117 def ChkReturnIsNonNeg(rc, emsg=None):
118  """ Check if return is non-negative.
119 
120  Raises a BotSenseError exception if false.
121 
122  Parameters:
123  rc - BotSense return code.
124  emsg - Optional error message string.
125  """
126  if rc < 0:
127  raise BotSenseError(rc, emsg)
128 ##
129 
130 
131 #------------------------------------------------------------------------------
132 # CLASS: BotSenseError
133 #------------------------------------------------------------------------------
134 class BotSenseError(Exception):
135  """ BotSense Exception Class. """
136 
137  #--
138  def __init__(self, ecode, emsg=None):
139  """ Raise exception.
140 
141  Parameters:
142  ecode - BotSense error code.
143  msg - Exception message string.
144  """
145  ## \h_botsense error code
146  self.ecode = ecode
147 
148  ## error message
149  self.emsg = emsg
150  ##
151 
152  #--
153  def __str__(self):
154  """ String representation. """
155  s = "%s(ecode=%d)" % (StrError(self.ecode), self.ecode)
156  if self.emsg:
157  s += ": %s" % (self.emsg)
158  return s
159  ##
160 ##
def __init__(self, ecode, emsg=None)
ecode
<b><i>BotSense</i></b> error code
def ChkReturnIsOk(rc, emsg=None)
def ChkReturnIsNonNeg(rc, emsg=None)