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: notice_error()
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: notice_error()
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: notice_error()
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() notice_error(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: notice_error()
def test_application_notice_error_params_not_a_dict(): try: raise RuntimeError() except RuntimeError: notice_error(sys.exc_info(), attributes=[1, 2, 3], application=application())
def test_application_exception_custom_params(): try: raise RuntimeError("one") except RuntimeError: application_instance = application() notice_error(attributes={"key": "value"}, application=application_instance)
def _nr_wrap_handle_exception(wrapped, instance, args, kwargs): response = wrapped(*args, **kwargs) notice_error(status_code=response.status_code) return response
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. notice_error(status_code=status_code) 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_application_exception_custom_params(): try: raise RuntimeError('one') except RuntimeError: application_instance = application() notice_error(attributes={'key': 'value'}, application=application_instance)
def test_multiple_error_events_outside_transaction(): app = application() for i in range(2): try: raise ErrorEventOutsideTransactionError(ERR_MESSAGE + str(i)) except ErrorEventOutsideTransactionError: notice_error(sys.exc_info(), application=app)
def test_notice_error_strip_message_disabled(): settings = application_settings() assert not settings.strip_exception_messages.enabled try: raise RuntimeError('one') except RuntimeError: notice_error()
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: notice_error()
def test_notice_error_strip_message_enabled(): settings = application_settings() assert settings.strip_exception_messages.enabled try: raise RuntimeError("message not displayed") except RuntimeError: notice_error()
def _function4(params=None, application=None): try: _function5() except: notice_error(attributes=(params or { 'err-key-2': 2, 'err-key-3': 3.0 }), application=application)
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: notice_error()
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) notice_error(status_code=status_code) return resp
def _test(): transaction = current_transaction() transaction._sampled = True with trace_type(*args): try: raise ValueError("whoops") except: notice_error()
def test_notice_error_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: notice_error()
def test_notice_error_strip_message_not_in_allowlist(): settings = application_settings() assert settings.strip_exception_messages.enabled assert _runtime_error_name not in settings.strip_exception_messages.allowlist try: raise RuntimeError("message not displayed") except RuntimeError: notice_error()
def test_span_event_notice_error_overrides_observed(trace_type, args): try: with trace_type(*args): try: raise ERROR except: notice_error() raise ValueError except ValueError: pass
def test_notice_error_multiple_different_type(): try: raise RuntimeError("one") except RuntimeError: notice_error() try: raise TypeError("two") except TypeError: notice_error()
def _wrapped(request, resolver, exc_info): transaction.set_transaction_name(name, priority=1) notice_error(exc_info) try: return wrapped(request, resolver, exc_info) except: # Catch all notice_error() raise
def test_notice_error_multiple_same_type(): try: raise RuntimeError("one") except RuntimeError: notice_error() try: raise RuntimeError("two") except RuntimeError: notice_error()
def test_notice_error_multiple_same_type(): try: raise RuntimeError('one') except RuntimeError: notice_error() try: raise RuntimeError('two') except RuntimeError: notice_error()
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: notice_error()
def test_py3_transaction_exception_message_bytes_non_english(): """An issue can occur if 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: notice_error()
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: notice_error(attributes=_err_param) else: notice_error() 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 notice_error() raise else: return self.__wrapped(*args, **kwargs)
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: notice_error()