Python thrift.Thrift.TException() Examples

The following are 12 code examples of thrift.Thrift.TException(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module thrift.Thrift , or try the search function .
Example #1
Source File: TMultiplexedProcessor.py    From galaxy-sdk-python with Apache License 2.0 6 votes vote down vote up
def process(self, iprot, oprot):
    (name, type, seqid) = iprot.readMessageBegin();
    if type != TMessageType.CALL & type != TMessageType.ONEWAY:
      raise TException("TMultiplex protocol only supports CALL & ONEWAY")

    index = name.find(TMultiplexedProtocol.SEPARATOR)
    if index < 0:
      raise TException("Service name not found in message name: " + name + ". Did you forget to use TMultiplexProtocol in your client?")

    serviceName = name[0:index]
    call = name[index+len(TMultiplexedProtocol.SEPARATOR):]
    if not serviceName in self.services:
      raise TException("Service name not found: " + serviceName + ". Did you forget to call registerProcessor()?")

    standardMessage = (
      call,
      type,
      seqid
    )
    return self.services[serviceName].process(StoredMessageProtocol(iprot, standardMessage), oprot) 
Example #2
Source File: thrift_connection.py    From lightstep-tracer-python with MIT License 6 votes vote down vote up
def open(self):
        """Establish HTTP connection to the server.

        Note: THttpClient also supports https and will use http/https according
        to the scheme in the URL it is given.
        """
        self._lock.acquire()
        try:
            self._transport = THttpClient.THttpClient(self._collector_url)
            self._transport.open()
            protocol = TBinaryProtocol.TBinaryProtocol(self._transport)
            self._client = ReportingService.Client(protocol)
        except Thrift.TException:
            self._open_exceptions_count += 1
        else:
            self.ready = True
        finally:
            self._lock.release()


    # May throw an Exception on failure. 
Example #3
Source File: TMultiplexedProcessor.py    From Protect4 with GNU General Public License v3.0 6 votes vote down vote up
def process(self, iprot, oprot):
        (name, type, seqid) = iprot.readMessageBegin()
        if type != TMessageType.CALL and type != TMessageType.ONEWAY:
            raise TException("TMultiplex protocol only supports CALL & ONEWAY")

        index = name.find(TMultiplexedProtocol.SEPARATOR)
        if index < 0:
            raise TException("Service name not found in message name: " + name + ". Did you forget to use TMultiplexProtocol in your client?")

        serviceName = name[0:index]
        call = name[index + len(TMultiplexedProtocol.SEPARATOR):]
        if serviceName not in self.services:
            raise TException("Service name not found: " + serviceName + ". Did you forget to call registerProcessor()?")

        standardMessage = (call, type, seqid)
        return self.services[serviceName].process(StoredMessageProtocol(iprot, standardMessage), oprot) 
Example #4
Source File: TMultiplexedProcessor.py    From Aditmadzs2 with GNU General Public License v3.0 6 votes vote down vote up
def process(self, iprot, oprot):
        (name, type, seqid) = iprot.readMessageBegin()
        if type != TMessageType.CALL and type != TMessageType.ONEWAY:
            raise TException("TMultiplex protocol only supports CALL & ONEWAY")

        index = name.find(TMultiplexedProtocol.SEPARATOR)
        if index < 0:
            raise TException("Service name not found in message name: " + name + ". Did you forget to use TMultiplexProtocol in your client?")

        serviceName = name[0:index]
        call = name[index + len(TMultiplexedProtocol.SEPARATOR):]
        if serviceName not in self.services:
            raise TException("Service name not found: " + serviceName + ". Did you forget to call registerProcessor()?")

        standardMessage = (call, type, seqid)
        return self.services[serviceName].process(StoredMessageProtocol(iprot, standardMessage), oprot) 
Example #5
Source File: TMultiplexedProcessor.py    From ajs2 with GNU General Public License v3.0 6 votes vote down vote up
def process(self, iprot, oprot):
        (name, type, seqid) = iprot.readMessageBegin()
        if type != TMessageType.CALL and type != TMessageType.ONEWAY:
            raise TException("TMultiplex protocol only supports CALL & ONEWAY")

        index = name.find(TMultiplexedProtocol.SEPARATOR)
        if index < 0:
            raise TException("Service name not found in message name: " + name + ". Did you forget to use TMultiplexProtocol in your client?")

        serviceName = name[0:index]
        call = name[index + len(TMultiplexedProtocol.SEPARATOR):]
        if serviceName not in self.services:
            raise TException("Service name not found: " + serviceName + ". Did you forget to call registerProcessor()?")

        standardMessage = (call, type, seqid)
        return self.services[serviceName].process(StoredMessageProtocol(iprot, standardMessage), oprot) 
Example #6
Source File: thrift_connection.py    From lightstep-tracer-python with MIT License 5 votes vote down vote up
def report(self, *args, **kwargs):
        """Report to the server."""
        # Notice the annoying case change on the method name. I chose to stay
        # consistent with casing in this class vs staying consistent with the
        # casing of the pass-through method.
        resp = None
        with self._lock:
            try:
                if self._client:
                    headers = {"Lightstep-Access-Token": args[0].access_token}
                    self._transport.setCustomHeaders(headers)
                    resp = self._client.Report(*args, **kwargs)
                    self._report_consecutive_errors = 0
            except Thrift.TException:
                self._report_consecutive_errors += 1
                self._report_exceptions_count += 1
                raise Exception('Thrift exception')
            except EOFError:
                self._report_consecutive_errors += 1
                self._report_eof_count += 1
                raise Exception('EOFError')
            finally:
                # In case the Thrift client has fallen into an unrecoverable state,
                # recreate the Thrift data structure if there are continued report
                # failures
                if self._report_consecutive_errors == CONSECUTIVE_ERRORS_BEFORE_RECONNECT:
                    self._report_consecutive_errors = 0
                    self.ready = False

        return resp 
Example #7
Source File: thrift.py    From splunk-elasticsearch with Apache License 2.0 5 votes vote down vote up
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
        request = RestRequest(method=Method._NAMES_TO_VALUES[method.upper()], uri=url,
                    parameters=params, body=body)

        start = time.time()
        tclient = None
        try:
            tclient = self._get_connection()
            response = tclient.execute(request)
            duration = time.time() - start
        except SocketTimeout as e:
            self.log_request_fail(method, url, body, time.time() - start, exception=e)
            raise ConnectionTimeout('TIMEOUT', str(e), e)
        except (TException, SocketTimeout) as e:
            self.log_request_fail(method, url, body, time.time() - start, exception=e)
            if tclient:
                try:
                    # try closing transport socket
                    tclient.transport.close()
                except Exception as e:
                    logger.warning(
                        'Exception %s occured when closing a failed thrift connection.',
                        e, exc_info=True
                    )
            raise ConnectionError('N/A', str(e), e)

        self._release_connection(tclient)

        if not (200 <= response.status < 300) and response.status not in ignore:
            self.log_request_fail(method, url, body, duration, response.status)
            self._raise_error(response.status, response.body)

        self.log_request_success(method, url, url, body, response.status,
            response.body, duration)

        headers = {}
        if response.headers:
            headers = dict((k.lower(), v) for k, v in response.headers.items())
        return response.status, headers, response.body or '' 
Example #8
Source File: thrift.py    From sublime-elasticsearch-client with MIT License 5 votes vote down vote up
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
        request = RestRequest(method=Method._NAMES_TO_VALUES[method.upper()], uri=url,
                    parameters=params, body=body)

        start = time.time()
        tclient = None
        try:
            tclient = self._get_connection()
            response = tclient.execute(request)
            duration = time.time() - start
        except SocketTimeout as e:
            self.log_request_fail(method, url, body, time.time() - start, exception=e)
            raise ConnectionTimeout('TIMEOUT', str(e), e)
        except (TException, SocketError) as e:
            self.log_request_fail(method, url, body, time.time() - start, exception=e)
            if tclient:
                try:
                    # try closing transport socket
                    tclient.transport.close()
                except Exception as e:
                    logger.warning(
                        'Exception %s occured when closing a failed thrift connection.',
                        e, exc_info=True
                    )
            raise ConnectionError('N/A', str(e), e)

        self._release_connection(tclient)

        if not (200 <= response.status < 300) and response.status not in ignore:
            self.log_request_fail(method, url, body, duration, response.status)
            self._raise_error(response.status, response.body)

        self.log_request_success(method, url, url, body, response.status,
            response.body, duration)

        headers = {}
        if response.headers:
            headers = dict((k.lower(), v) for k, v in response.headers.items())
        return response.status, headers, response.body or '' 
Example #9
Source File: thrift_client_mixin.py    From kingpin with Apache License 2.0 5 votes vote down vote up
def default_thrift_client_replace_if(exception):
    """Default policy on whether to replace the underlying thrift client.

    The default policy tests whether the given exception is a user defined
    thrift exception. If the given exception is a user defined thrift exception,
    it indicates the underlying connection to the thrift server is sound and can
    continue to be used.

    Args:
        exception: Exception thrown from method invocation of the thrift client.

    Return:
        True, if the underlying thrift client needs to be replaced; otherwise,
        False will be returned.

    """
    # NOTE: this is the best I can come up with to exam whether the given
    # exception is a user defined exception or not. Currently
    # TApplicationException, TProtocolException, TTransportException, and user
    # defined thrift exceptions are all subclasses of TException; however, the
    # other types of exceptions besides user defined ones don't have thrift_spec
    # attribute
    if isinstance(exception, TException):
        if (isinstance(exception, TApplicationException) or
                hasattr(exception, 'thrift_spec')):
            return False
    return True 
Example #10
Source File: client.py    From hazelcast-python-client with Apache License 2.0 5 votes vote down vote up
def __init__(self, host, port):
        try:
            # Make socket
            transport = TSocket.TSocket(host=host, port=port)
            # Buffering is critical. Raw sockets are very slow
            transport = TTransport.TBufferedTransport(transport)
            # Wrap in a protocol
            protocol = TBinaryProtocol.TBinaryProtocol(transport)
            self.remote_controller = RemoteController.Client(protocol)
            # Connect!
            transport.open()
        except Thrift.TException as tx:
            self.logger.warn('%s' % tx.message) 
Example #11
Source File: ThriftTest.py    From tchannel-python with MIT License 5 votes vote down vote up
def testException(self, arg):
        """
        Print 'testException(%s)' with arg as '%s'
        @param string arg - a string indication what type of exception to throw
        if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
        elsen if arg == "TException" throw TException
        else do not throw anything

        Parameters:
         - arg

        """
        pass 
Example #12
Source File: ThriftTest.py    From tchannel-python with MIT License 5 votes vote down vote up
def testException(self, arg):
        """
        Print 'testException(%s)' with arg as '%s'
        @param string arg - a string indication what type of exception to throw
        if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
        elsen if arg == "TException" throw TException
        else do not throw anything

        Parameters:
         - arg

        """
        self.send_testException(arg)
        self.recv_testException()