def test_py3_transaction_exception_message_bytes_non_english_unicode(): """Assert (native) unicode exception message is preserved when when non-ascii compatible characters present""" try: raise ValueError(UNICODE_MESSAGE) except ValueError: record_exception()
def _function4(params=None, application=None): try: _function5() except: record_exception(params=(params or { 'err-key-2': 2, 'err-key-3': 3.0}), application=application)
def test_py2_application_exception_message_bytes_english(): """Assert byte string of ascii characters decodes sensibly""" try: raise ValueError(BYTES_ENGLISH) except ValueError: app = application() record_exception(application=app)
def test_py3_transaction_exception_message_unicode_english(): """Assert (native) unicode exception message is preserved, when characters are ascii-compatible""" try: raise ValueError(UNICODE_ENGLISH) except ValueError: record_exception()
def test_py2_transaction_exception_message_unicode_english(): """Assert unicode message when using ascii compatible characters preserved, with sys default encoding""" try: raise ValueError(UNICODE_ENGLISH) except ValueError: record_exception()
def test_py2_transaction_exception_message_unicode(): """Assert unicode message when using non-ascii characters is preserved, with sys default encoding""" try: raise ValueError(UNICODE_MESSAGE) except ValueError: record_exception()
def test_multiple_error_events_outside_transaction(): app = application() for i in range(2): try: raise ErrorEventOutsideTransactionError(ERR_MESSAGE + str(i)) except ErrorEventOutsideTransactionError: record_exception(*sys.exc_info(), application=app)
def test_application_exception_custom_params(): try: raise RuntimeError('one') except RuntimeError: application_instance = application() record_exception(params={'key': 'value'}, application=application_instance)
def wrapper_RoutesDispatcher_find_handler(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) try: # Call the original wrapped function to find the handler. handler = wrapped(*args, **kwargs) except: # Catch all # Can end up here when the URL was invalid in some way. record_exception() raise if handler: # Should be the actual handler, wrap it with the handler # wrapper. handler = handler_wrapper(handler) else: # No handler could be found so name the web transaction # after the 404 status code. transaction.set_transaction_name('404', group='StatusCode') return handler
def test_record_exception_strip_message_enabled(): settings = application_settings() assert settings.strip_exception_messages.enabled try: raise RuntimeError('message not displayed') except RuntimeError: record_exception()
def _nr_wrap_handle_exception(wrapped, instance, args, kwargs): response = wrapped(*args, **kwargs) if not ignore_status_code(response.status_code): record_exception() return response
def record_response_error(response, value): status_code = getattr(response, "status_code", None) exc = getattr(value, "__class__", None) tb = getattr(value, "__traceback__", None) if ignore_status_code(status_code): value._nr_ignored = True else: record_exception(exc, value, tb)
def test_py2_transaction_exception_message_unicode_utf8_encoding(): """Assert unicode error message is preserved with sys non-default utf-8 encoding """ try: raise ValueError(UNICODE_MESSAGE) except ValueError: record_exception()
def _test(): transaction = current_transaction() transaction._sampled = True with trace_type(*args): try: raise ValueError("whoops") except: record_exception()
def test_record_exception_strip_message_not_in_whitelist(): settings = application_settings() assert settings.strip_exception_messages.enabled assert _runtime_error_name not in settings.strip_exception_messages.whitelist try: raise RuntimeError('message not displayed') except RuntimeError: record_exception()
def test_py2_transaction_exception_message_bytes_non_english(): """Assert known situation where (explicitly) utf-8 encoded byte string gets mangled when default sys encoding is ascii. THIS TEST ASSERTS THAT THE MESSAGE IS WRONG. We do not expect it to work now, or in the future. """ try: raise ValueError(BYTES_UTF8_ENCODED) except ValueError: record_exception()
def __exit__(self, exc, value, tb): if exc is None or value is None or tb is None: return if self._transaction is None: return record_exception(exc=exc, value=value, tb=tb, ignore_errors=self._ignore_errors)
def test_record_exception_strip_message_in_whitelist(): settings = application_settings() assert settings.strip_exception_messages.enabled assert _runtime_error_name in settings.strip_exception_messages.whitelist try: raise RuntimeError('original error message') except RuntimeError: record_exception()
def _nr_wrap_Api_handle_error_(wrapped, instance, args, kwargs): # If calling wrapped raises an exception, the error will bubble up to # flask's exception handler and we will capture it there. resp = wrapped(*args, **kwargs) record_exception(ignore_errors=should_ignore) return resp
def _wrapped(request, resolver, exc_info): transaction.set_transaction_name(name, priority=1) record_exception(*exc_info) try: return wrapped(request, resolver, exc_info) except: # Catch all record_exception() raise
def test_span_event_record_exception_overrides_observed(trace_type, args): try: with trace_type(*args): try: raise ERROR except: record_exception() raise ValueError except ValueError: pass
def test_record_exception_multiple_different_type(): try: raise RuntimeError('one') except RuntimeError: record_exception() try: raise TypeError('two') except TypeError: record_exception()
def test_record_exception_multiple_same_type(): try: raise RuntimeError('one') except RuntimeError: record_exception() try: raise RuntimeError('two') except RuntimeError: record_exception()
def test_py2_transaction_exception_message_bytes_utf8_encoding_non_english(): """Assert utf-8 encoded byte produces correct exception message when sys encoding is also utf-8. """ try: # Bytes literal with non-ascii compatible characters only allowed in # python 2 raise ValueError('I💜🐍') except ValueError: record_exception()
def test_py3_transaction_exception_message_bytes_non_english(): """It really makes a mess of things when you cast from bytes to a string in python 3 (that is using str(), not using encode/decode methods). This is because all characters in bytes are literals, no implicit decoding happens, like it does in python 2. You just shouldn't use bytes for exception messages in Python 3. THIS TEST ASSERTS THAT THE MESSAGE IS WRONG. We do not expect it to work now, or in the future. """ try: raise ValueError(BYTES_UTF8_ENCODED) except ValueError: record_exception()
def fully_featured_app(environ, start_response): status = '200 OK' path = environ.get('PATH_INFO') use_user_attrs = environ.get('record_attributes', 'TRUE') == 'TRUE' environ['wsgi.input'].read() environ['wsgi.input'].readline() environ['wsgi.input'].readlines() if use_user_attrs: for attr, val in _custom_parameters.items(): add_custom_parameter(attr, val) if 'db' in environ and int(environ['db']) > 0: connection = db.connect(":memory:") for i in range(int(environ['db']) - 1): connection.execute("create table test_db%d (a, b, c)" % i) if 'external' in environ: for i in range(int(environ['external'])): r = urlopen('http://www.python.org') r.read(10) if 'err_message' in environ: n_errors = int(environ.get('n_errors', 1)) for i in range(n_errors): try: # append number to stats engine to get unique errors, so they # don't immediately get filtered out. raise ValueError(environ['err_message'] + str(i)) except ValueError: if use_user_attrs: record_exception(params=_err_param) else: record_exception() text = '<html><head>%s</head><body><p>RESPONSE</p>%s</body></html>' output = (text % (get_browser_timing_header(), get_browser_timing_footer())).encode('UTF-8') response_headers = [('Content-type', 'text/html; charset=utf-8'), ('Content-Length', str(len(output)))] write = start_response(status, response_headers) write(b'') return [output]
def __call__(self, *args, **kwargs): current_transaction = newrelic.api.transaction.current_transaction() if current_transaction: webob_exc = newrelic.api.import_hook.import_module('webob.exc') try: return self.__wrapped(*args, **kwargs) except webob_exc.HTTPException: raise except: # Catch all record_exception() raise else: return self.__wrapped(*args, **kwargs)
def wrap_finish(wrapped, instance, args, kwargs): try: return wrapped(*args, **kwargs) finally: transaction = getattr(instance, '_nr_transaction', None) if transaction: start_time = getattr(transaction, '_async_start_time', None) if start_time: trace_cache().record_event_loop_wait(start_time, time.time()) transaction._async_start_time = None record_exception(*sys.exc_info(), ignore_errors=should_ignore) transaction.__exit__(None, None, None) instance._nr_transaction = None
def test_py2_transaction_exception_message_bytes_implicit_encoding_non_english(): """Assert known situation where (implicitly) utf-8 encoded byte string gets mangled when default sys encoding is ascii. THIS TEST ASSERTS THAT THE MESSAGE IS WRONG. We do not expect it to work now, or in the future. """ try: # Bytes literal with non-ascii compatible characters only allowed in # python 2 raise ValueError('I💜🐍') except ValueError: record_exception()
def __call__(self, request, response, session): txn = newrelic.api.transaction.current_transaction() if txn: HTTP = newrelic.api.import_hook.import_module('gluon.http').HTTP try: return self._nr_next_object(request, response, session) except HTTP: raise except: # Catch all record_exception() raise else: return self._nr_next_object(request, response, session)