def send(self, data, headers, timeout=None): """ Sends a request to a remote webserver using HTTP POST. """ req = Request(self._url, headers=headers) if timeout is None: timeout = defaults.TIMEOUT try: try: response = urlopen(req, data, timeout) except TypeError: response = urlopen(req, data) except Exception as e: if isinstance(e, socket.timeout): message = ("Connection to Opbeat server timed out " "(url: %s, timeout: %d seconds)" % (self._url, timeout)) elif isinstance(e, HTTPError): body = e.read() message = ('Unable to reach Opbeat server: ' '%s (url: %s, body: %s)' % (e, self._url, body)) else: message = 'Unable to reach Opbeat server: %s (url: %s)' % ( e, self._url) raise TransportException(message, data) return response
def test_send_remote_failover_async(self, should_try, http_send): should_try.return_value = True client = Client( servers=['http://example.com'], organization_id='organization_id', app_id='app_id', secret_token='secret', async_mode=True, ) logger = mock.Mock() client.error_logger.error = logger # test error encoded_data = client.encode({'message': 'oh no'}) http_send.side_effect = TransportException('oopsie', encoded_data) client.send_remote('http://example.com/api/store', data=encoded_data) client.close() assert client.state.status == client.state.ERROR assert len(logger.call_args_list) == 2 assert 'oopsie' in logger.call_args_list[0][0][0] assert 'oh no' in logger.call_args_list[1][0][1] # test recovery http_send.side_effect = None client.send_remote('http://example.com/api/store', 'foo') client.close() assert client.state.status == client.state.ONLINE
def send(self, data, headers, timeout=None): if timeout is None: timeout = defaults.TIMEOUT response = None try: try: response = self.http.urlopen('POST', self._url, body=data, headers=headers, timeout=timeout) except Exception as e: print_trace = True if isinstance(e, MaxRetryError) and isinstance( e.reason, TimeoutError): message = ("Connection to Opbeat server timed out " "(url: %s, timeout: %d seconds)" % (self._url, timeout)) print_trace = False else: message = 'Unable to reach Opbeat server: %s (url: %s)' % ( e, self._url) raise TransportException(message, data, print_trace=print_trace) body = response.read() if response.status >= 400: if response.status == 429: # rate-limited message = 'Temporarily rate limited: ' print_trace = False else: message = 'HTTP %s: ' % response.status print_trace = True message += body.decode('utf8') raise TransportException(message, data, print_trace=print_trace) return response.getheader('Location') finally: if response: response.close()
def send(self, data, headers, timeout=None): """ Sends a request to a remote webserver using HTTP POST. Returns the shortcut URL of the recorded error on Opbeat """ req = Request(self._url, headers=headers) if timeout is None: timeout = defaults.TIMEOUT response = None try: try: response = urlopen(req, data, timeout) except TypeError: response = urlopen(req, data) except Exception as e: print_trace = True if isinstance(e, socket.timeout): message = ("Connection to Opbeat server timed out " "(url: %s, timeout: %d seconds)" % (self._url, timeout)) elif isinstance(e, HTTPError): body = e.read() if e.code == 429: # rate-limited message = 'Temporarily rate limited: ' print_trace = False else: message = 'Unable to reach Opbeat server: ' message += '%s (url: %s, body: %s)' % (e, self._url, body) else: message = 'Unable to reach Opbeat server: %s (url: %s)' % ( e, self._url) raise TransportException(message, data, print_trace=print_trace) finally: if response: response.close() return response.info().get('Location')
async def send(self, data, headers, timeout): from opbeat.transport.base import TransportException self.data = data if self.url == urlparse('http://error'): raise TransportException('', data, False) await asyncio.sleep(0.0001)