def _relay_error(self, failure, try_relays, command): failure.trap(RelayError) failed_relay = try_relays.popleft() failed_relay.logger.warning( 'The {0.name!r} request failed: {1.value}'.format( command, failure)) return self._try_next(try_relays, command)
def error_callback(self,failure,payload): try: failure.trap(CommandException) # the next line will only run if failure is a CommandException self.args = payload except twisted.python.failure.Failure: pass
def _trap_nosuchcodeimportjob(self, failure): failure.trap(xmlrpc.Fault) if failure.value.faultCode == NoSuchCodeImportJob.error_code: self._call_finish_job = False raise ExitQuietly else: raise failure.value
def error(failure, try_again=0): if try_again is not 0: failure.trap(BufferedCommandFailed) d = task.deferLater(reactor, 0.2, fn, id, command) d.addCallbacks(next, error, errbackKeywords={"try_again": try_again - 1}) return d
def _errback(self, failure: Failure) -> None: # This gets called on any exceptions in aiterator.__anext__(). # It handles StopAsyncIteration by stopping the iteration and reraises all others. self.anext_deferred = None failure.trap(StopAsyncIteration) self.finished = True for d in self.waiting_deferreds: d.callback(None)
def _sessionClosed(self, maybeFailure, sessionID): del self.sessions[sessionID] if isinstance(maybeFailure, failure.Failure): failure.trap(error.ConnectionDone, error.ConnectionLost, SessionTimeout) else: return maybeFailure
def err_overload(self, failure, message_type): """Handle database overloads Pause producing to cease incoming notifications while we wait a random interval up to 8 seconds before closing down the connection. Most clients wait up to 10 seconds for a command, but this is not a guarantee, so rather than never reply, we still shut the connection down. """ failure.trap(ProvisionedThroughputExceededException) self.transport.pauseProducing() d = self.deferToLater(random.randrange(4, 9), self.err_finish_overload, message_type) d.addErrback(self.trap_cancel)
def relookup(failure): failure.trap(error.DNSNameError) if type == dns.PTR: ip = self._reverse_ip(name) d = threads.deferToThread(self._ec2.get_all_instances, filters={self.reverse_filter: ip}) d.addCallback(self.create_message, name, self.forward_filter, record=dns.PTR) elif type == dns.A: d = threads.deferToThread(self._ec2.get_all_instances, filters={self.forward_filter: str(name)}) d.addCallback(self.create_message, name, self.reverse_filter, record=dns.A) else: raise ValueError, "Record constant '%s' is not supported" % (type) return d
def _retry_local_port(self, failure): """ Handles :api:`twisted.internet.error.CannotListenError` by trying again on another port. After 10 failures, we give up and propogate the error. """ failure.trap(error.CannotListenError) self.retries += 1 if self.retries > 10: return failure self.listen_port = self.port_generator() ## we do want to overwrite the whole list, not append self.hiddenservice.ports = ['%d 127.0.0.1:%d' % (self.public_port, self.listen_port)] d = self.config.save() d.addCallback(self._create_listener).addErrback(self._retry_local_port) return d
def _label_helper(self, failure, debug=False): """ Errback, который ловит L{SkipToProcess}, чтобы обеcпечить остановку на нужном label после skip to. @param debug: включить отладочный режим? @type debug: C{bool} """ failure.trap(SkipToProcess) if failure.value.label != self.label: if debug: failure.value.pack.debug("Labels for skip don't match %d != %d" % (failure.value.label, self.label)) return failure if debug: failure.value.pack.debug("Stopped skip to at label %d" % self.label) return failure.value.pack
def _ebRoundRobinBackoff(self, failure, fakeProto): failure.trap(defer.TimeoutError) # Assert that each server is tried with a particular timeout # before the timeout is increased and the attempts are repeated. for t in (1, 3, 11, 45): tries = fakeProto.queries[: len(self.testServers)] del fakeProto.queries[: len(self.testServers)] tries.sort() expected = list(self.testServers) expected.sort() for ((addr, query, timeout, id), expectedAddr) in zip(tries, expected): self.assertEqual(addr, (expectedAddr, 53)) self.assertEqual(timeout, t) self.assertFalse(fakeProto.queries)
def test_invalid_domain(self): """Tests sending to a domain we don't have mappings for.""" rcpt = smtp.User("*****@*****.**", None, object(), self.sender) ret = self._sendMessage(rcpt, False) ret.addCallbacks(self.fail, lambda failure: failure.trap(smtp.SMTPBadRcpt)) ret.addCallback(self._checkLogs, [("user", "invaliddomain.com", logging.ERROR, str(self.sender), str(rcpt.dest), 0)]) return ret
def _ebRoundRobinBackoff(self, failure, fakeProto): failure.trap(defer.TimeoutError) # Assert that each server is tried with a particular timeout # before the timeout is increased and the attempts are repeated. for t in (1, 3, 11, 45): tries = fakeProto.queries[:len(self.testServers)] del fakeProto.queries[:len(self.testServers)] tries.sort() expected = list(self.testServers) expected.sort() for ((addr, query, timeout, id), expectedAddr) in zip(tries, expected): self.assertEquals(addr, (expectedAddr, 53)) self.assertEquals(timeout, t) self.failIf(fakeProto.queries)
def loginFailed(failure): def gotRoot(root): # an idempotent method, should return a network failure if # the remote side disconnects as it should return root.callRemote("getKeycardClasses") def gotError(failure): self.assertAdditionalFDsOpen(1, "feedSent (socket)") self.info("success") def gotKeycardClasses(classes): raise AssertionError("should not get here") self.info("loginFailed: %s", log.getFailureMessage(failure)) failure.trap(errors.NotAuthenticatedError) d = factory.getRootObject() # should fire immediately d.addCallback(gotRoot) d.addCallbacks(gotKeycardClasses, gotError) return d
def loginFailed(failure): def gotRoot(root): # an idempotent method, should return a network failure if # the remote side disconnects as it should return root.callRemote('getKeycardClasses') def gotError(failure): self.assertAdditionalFDsOpen(1, 'feedSent (socket)') self.info('success') def gotKeycardClasses(classes): raise AssertionError('should not get here') self.info('loginFailed: %s', log.getFailureMessage(failure)) failure.trap(errors.NotAuthenticatedError) d = factory.getRootObject() # should fire immediately d.addCallback(gotRoot) d.addCallbacks(gotKeycardClasses, gotError) return d
def test_invalid_address(self): """Tests sending to an invalid address.""" rcpt = smtp.User("*****@*****.**", None, object(), self.sender) ret = self._sendMessage(rcpt, False) ret.addCallbacks(self.fail, lambda failure: failure.trap(smtp.SMTPBadRcpt)) ret.addCallback(self._checkLogs, [("doesnotexist", "smtp2web.com", logging.ERROR, str(self.sender), str(rcpt.dest), 0)]) return ret
def test_invalid_domain(self): """Tests sending to a domain we don't have mappings for.""" rcpt = smtp.User("*****@*****.**", None, object(), self.sender) ret = self._sendMessage(rcpt, False) ret.addCallbacks( self.fail, lambda failure: failure.trap(smtp.SMTPBadRcpt) ) ret.addCallback(self._checkLogs, [("user", "invaliddomain.com", logging.ERROR, str(self.sender), str(rcpt.dest), 0)]) return ret
def test_invalid_address(self): """Tests sending to an invalid address.""" rcpt = smtp.User("*****@*****.**", None, object(), self.sender) ret = self._sendMessage(rcpt, False) ret.addCallbacks( self.fail, lambda failure: failure.trap(smtp.SMTPBadRcpt) ) ret.addCallback(self._checkLogs, [("doesnotexist", "smtp2web.com", logging.ERROR, str(self.sender), str(rcpt.dest), 0)]) return ret
def test_send_fail(self): """Tests sending with a handler that raises an exception.""" rcpt = smtp.User("*****@*****.**", None, object(), self.sender) ret = self._sendMessage(rcpt) ret.addCallbacks( self.fail, lambda failure: failure.trap(smtp2web.MessageSubmissionError) ) handler = self.settings.usermap['smtp2web.com'].findHandler("fail") ret.addCallback(lambda x: self.failUnlessEqual(handler.invocations, [(str(self.sender), str(rcpt.dest), self.test_message)])) ret.addCallback(self._checkLogs, [("fail", "smtp2web.com", logging.ERROR, str(self.sender), str(rcpt.dest), len(self.test_message))]) return ret
def relookup(failure): failure.trap(error.DNSNameError) if type == dns.PTR: ip = self._reverse_ip(name) d = threads.deferToThread(self._ec2.get_all_instances, filters={self.reverse_filter: ip}) d.addCallback(self.create_message, name, self.forward_filter, record=dns.PTR) elif type == dns.A: d = threads.deferToThread( self._ec2.get_all_instances, filters={self.forward_filter: str(name)}) d.addCallback(self.create_message, name, self.reverse_filter, record=dns.A) else: raise ValueError, "Record constant '%s' is not supported" % ( type) return d
def __checkJobStepResults(self, failure, status): "Check that given self.js.status is set to correct value after certain JobStep operation" twisted_logger.writeLog(self.logPrefix, None, "Checking test results...") if failure is not None: error = failure.getErrorMessage() failure.trap(RuntimeError) print "ERROR: %s" % error else: error = None query = "select status, text_input, error, start_time, end_time from scs.job_step " \ "where job_id = %d and step_id = %d" % (self.js.jobID, self.js.stepID) res = synchdb.query(query) db_status, text_input, db_error, start_time, end_time = res[0] self.assertEquals(str(self.js.input), text_input, "<scs.job_step> text_input ('%s') not equals to expected input ('%s')" % (text_input, str(self.js.input))) self.assertEquals(error, db_error, "<scs.job_step> error ('%s') not equals to expected error ('%s')" % (db_error, error)) self.assertEquals(status, db_status, "<scs.job_step> status ('%s') not equals to expected status ('%s')" % (db_status, status)) self.assertEquals(self.js.status, status, "<js.status> ('%s') not equals to expected status ('%s')" % (self.js.status, status)) if self.js.status != 'RUNNING': self.assertNotEquals(end_time, None) twisted_logger.writeLog(self.logPrefix, None, "Test succeeded!")
def __call__(self, failure): r = failure.trap(weberror.Error,error.ConnectionRefusedError) if r == error.ConnectionRefusedError: status = int(http.INTERNAL_SERVER_ERROR) else: status = int(failure.value.status) if status == http.NOT_FOUND: if self.seen404: return failure else: self.seen404 = True elif status not in self.okErrs: return failure
def test_send_fail(self): """Tests sending with a handler that raises an exception.""" rcpt = smtp.User("*****@*****.**", None, object(), self.sender) ret = self._sendMessage(rcpt) ret.addCallbacks( self.fail, lambda failure: failure.trap(smtp2web.MessageSubmissionError)) handler = self.settings.usermap['smtp2web.com'].findHandler("fail") ret.addCallback(lambda x: self.failUnlessEqual(handler.invocations, [( str(self.sender), str(rcpt.dest), self.test_message)])) ret.addCallback( self._checkLogs, [("fail", "smtp2web.com", logging.ERROR, str( self.sender), str(rcpt.dest), len(self.test_message))]) return ret
def maybe_make_branch_in_db(failure): # Looks like we are trying to make a branch. failure.trap(NoSuchFile) return self.server.createBranch(self._abspath(relpath))
def _error_handler(self, failure): failure.trap(CommandError, RelayError) self.logger.error(failure.value) self.reply('error')
def defaultErrback(self, failure): """TODO better network error handling. """ failure.trap(ConnectionAborted, ConnectionClosed, ConnectionDone, ConnectionLost)
def handleAttributeKeyError(failure): """ Обрабатываем исключение об отсутствии атрибута у сообщения. """ failure.trap(AttributeNotFoundError) raise errors.MessageAttributeKeyException, failure.value.args[0]
def _remote_abort_handler_eb(self, failure): failure.trap(CallAbortedException) # log failure but do nothing more log.debug('remote call aborted: %s', failure.value)
def _eb_no_srv(self, failure, addr, port): failure.trap(DomainError) return reactor.resolve(addr).addCallback(lambda host: (host, port)).addErrback(self._eb_no_dns, addr)
def __call__(self, failure): # AuthoritativeDomainErrors should halt resolution attempts failure.trap(dns.DomainError, defer.TimeoutError, NotImplementedError) return self.resolver(self.query, self.timeout, self.addr, self.edns)
def _authenticatedErrback(self, failure, request): failure.trap(errors.NotAuthenticatedError) self._handleUnauthorized(request, http.UNAUTHORIZED) return failure
def eb(failure): if failure.trap(errors.ComponentMoodError): common.errorRaise("Component '%s' is in the wrong mood." % self.parentCommand.componentId) else: common.errorRaise(log.getFailureMessage(failure))
def _errback(self, failure): failure.trap(errors.SleepingComponentError) common.errorRaise("Component '%s' is sleeping." % self.parentCommand.componentId)
def try_second_key(failure): failure.trap(UnauthorizedLogin) return checker.requestAvatarId(right_key_creds)
def _silenceQuietExit(self, failure): """Quietly swallow a ExitQuietly failure.""" failure.trap(ExitQuietly) return None
def checkConnection(failure): failure.trap(ConnectionRefusedError) print "trying next proxy if available" self.__class__._needNextProxy = True return Service.__call__(self, *params).addErrback(checkConnection)
def processSkipThrough(failure): """ Обрабатываем ситуацию, когда для skip to.. не нашлось метки. """ failure.trap(SkipToProcess) raise errors.SkipToFallthroughError(failure.value.label)
def handleEOF(self, failure, remoteFd, localFd): failure.trap(exceptions.EOFError) #self.factory.runner.log("download completed") return remoteFd
def noPostProcessor(failure, report): failure.trap(e.NoPostProcessor) return report
def _checkFailure(failure): failure.trap(smtp.SMTPServerError) self.failUnlessEqual(failure.value.code, 552)
def p(failure): failure.trap(ftp.CommandFailed)
def defaultErrback(self, failure): #see testing note above failure.trap(ConnectionAborted, ConnectionClosed, ConnectionDone, ConnectionLost) #pragma: no cover
def handleDomainKeyError(failure): """ Обрабатываем отсутствующий ключ в домене, на который "напоролось" правило. """ failure.trap(DomainKeyError) raise errors.AttributeKeyException, failure.value.args[0]
def defaultErrback(self, failure): failure.trap(ConnectionAborted, ConnectionClosed, ConnectionDone, ConnectionLost)
def checkNotImplemented(failure): failure.trap(NotImplementedError) raise error.StanzaError('feature-not-implemented')
def _eb_no_dns(self, failure, addr): failure.trap(DNSLookupError) log.error("Could resolve neither SRV nor A record for '%s'" % addr)
def p(failure): # Make sure we got the failure we were expecting. failure.trap(ftp.CommandFailed)
def _ebLogin(failure): failure.trap(cred_error.UnauthorizedLogin, cred_error.UnhandledCredentials) self.state = self.UNAUTH raise AuthorizationError
def error (failure, try_again = 0): if try_again is not 0: failure.trap(BufferedCommandFailed) d = task.deferLater(reactor, 0.2, fn, id, command) d.addCallbacks(next, error, errbackKeywords = { "try_again": try_again - 1 }) return d
def trap(self, failure, what): failure.trap(what)
def _eb_no_srv(self, failure, addr, port): failure.trap(DomainError) return reactor.resolve(addr).addCallback( lambda host: (host, port)).addErrback(self._eb_no_dns, addr)
def defaultErrback(self, failure): failure.trap(ConnectionAborted, ConnectionClosed, ConnectionDone, ConnectionLost, UnknownRemoteError) reactor.stop()
def _authenticatedErrback(self, failure, request): failure.trap(errors.UnknownComponentError, errors.NotAuthenticatedError) self._handleUnauthorized(request, http.UNAUTHORIZED) return failure
def fromStanzaError(failure, iq): failure.trap(error.StanzaError) return failure.value.toResponse(iq)