Advertisement
felipeo

Capturing and re-raising failures

Jun 28th, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. ############ 1st Attempt ############
  2.     def connectionLost(self, reason):
  3.         if self.dead: # Only set by callLater used to time out
  4.             self.factory.clientConnectionFailed(
  5.                 self.transport,
  6.                 Failure(TimeOutError(self.factory.timeout),
  7.                         TimeOutError,
  8.                         reason.getTracebackObject())) # also tried reason.tb  here :(
  9.         else:
  10.             self.killer.cancel()
  11.             self.poemReceived(self.poem)
  12.  
  13. ############ 2nd Attempt: ###########
  14.     def connectionLost(self, reason):
  15.         if self.dead:
  16.             try:
  17.                 try:
  18.                     reason.raiseException()
  19.                 except: # This makes me very uncomfortable!
  20.                     raise TimeOutError(self.factory.timeout)
  21.             except TimeOutError:
  22.                 self.factory.clientConnectionFailed(self.transport,
  23.                                                 Failure())
  24.         else:
  25.             self.killer.cancel()
  26.             self.poemReceived()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement