Beispiel #1
0
 def test_null_values(self):
     sink = {}
     assert Exception.to_python({}).to_json() == sink
     assert Exception.to_python({'exc_omitted': None}).to_json() == sink
     assert Exception.to_python({'values': None}).to_json() == sink
     assert Exception.to_python({'values': []}).to_json() == sink
     assert Exception.to_python({'values': [None]}).to_json() == {"values": [None]}
Beispiel #2
0
    def test_context_with_mixed_frames(self):
        inst = Exception.to_python(dict(values=[{
            'type': 'ValueError',
            'value': 'hello world',
            'module': 'foo.bar',
            'stacktrace': {'frames': [{
                'filename': 'foo/baz.py',
                'lineno': 1,
                'in_app': True,
            }]},
        }, {
            'type': 'ValueError',
            'value': 'hello world',
            'module': 'foo.bar',
            'stacktrace': {'frames': [{
                'filename': 'foo/baz.py',
                'lineno': 1,
                'in_app': False,
            }]},
        }]))

        self.create_event(data={
            'sentry.interfaces.Exception': inst.to_json(),
        })
        context = inst.get_api_context()
        assert context['hasSystemFrames']
    def test_context_with_only_app_frames(self):
        inst = Exception.to_python(dict(values=[{
            'type': 'ValueError',
            'value': 'hello world',
            'module': 'foo.bar',
            'stacktrace': {'frames': [{
                'filename': 'foo/baz.py',
                'lineno': 1,
                'in_app': True,
            }]},
        }, {
            'type': 'ValueError',
            'value': 'hello world',
            'module': 'foo.bar',
            'stacktrace': {'frames': [{
                'filename': 'foo/baz.py',
                'lineno': 1,
                'in_app': True,
            }]},
        }]))

        event = self.create_event(data={
            'sentry.interfaces.Exception': inst.to_json(),
        })
        context = inst.get_context(event)
        assert context['system_frames'] == 0
        assert context['exceptions'][0]['stacktrace']['system_frames'] == 0
        assert context['exceptions'][1]['stacktrace']['system_frames'] == 0
Beispiel #4
0
    def test_context_with_mechanism(self):
        inst = Exception.to_python(
            dict(
                values=[
                    {
                        'type': 'ValueError',
                        'value': 'hello world',
                        'module': 'foo.bar',
                        'stacktrace': {
                            'frames': [{
                                'filename': 'foo/baz.py',
                                'lineno': 1,
                                'in_app': True,
                            }]
                        },
                        'mechanism': {
                            'type': 'generic',
                        }
                    }
                ]
            )
        )

        self.create_event(data={
            'exception': inst.to_json(),
        })
        context = inst.get_api_context()
        assert context['values'][0]['mechanism']['type'] == 'generic'
Beispiel #5
0
    def test_context_with_mixed_frames(self):
        inst = Exception.to_python(
            dict(values=[{
                'type': 'ValueError',
                'value': 'hello world',
                'module': 'foo.bar',
                'stacktrace': {
                    'frames': [{
                        'filename': 'foo/baz.py',
                        'lineno': 1,
                        'in_app': True,
                    }]
                },
            }, {
                'type': 'ValueError',
                'value': 'hello world',
                'module': 'foo.bar',
                'stacktrace': {
                    'frames': [{
                        'filename': 'foo/baz.py',
                        'lineno': 1,
                        'in_app': False,
                    }]
                },
            }]))

        self.create_event(data={
            'exception': inst.to_json(),
        })
        context = inst.get_api_context()
        assert context['hasSystemFrames']
Beispiel #6
0
    def test_context_with_raw_stacks(self):
        inst = Exception.to_python(dict(values=[{
            'type': 'ValueError',
            'value': 'hello world',
            'module': 'foobar',
            'raw_stacktrace': {'frames': [{
                'filename': None,
                'lineno': 1,
                'function': '<redacted>',
                'in_app': True,
            }]},
            'stacktrace': {'frames': [{
                'filename': 'foo/baz.c',
                'lineno': 1,
                'function': 'main',
                'in_app': True,
            }]},
        }]))

        self.create_event(data={
            'sentry.interfaces.Exception': inst.to_json(),
        })
        context = inst.get_api_context()
        assert context['values'][0]['stacktrace']['frames'][0]['function'] == 'main'
        assert context['values'][0]['rawStacktrace']['frames'][0]['function'] == '<redacted>'
Beispiel #7
0
def chained_exception(interface: ChainedException, event: Event,
                      context: GroupingContext,
                      **meta: Any) -> ReturnedVariants:
    # Case 1: we have a single exception, use the single exception
    # component directly to avoid a level of nesting
    exceptions = interface.exceptions()
    if len(exceptions) == 1:
        return context.get_grouping_component(exceptions[0],
                                              event=event,
                                              **meta)

    # Case 2: produce a component for each chained exception
    by_name: Dict[str, List[GroupingComponent]] = {}

    for exception in exceptions:
        for name, component in context.get_grouping_component(exception,
                                                              event=event,
                                                              **meta).items():
            by_name.setdefault(name, []).append(component)

    rv = {}

    for name, component_list in by_name.items():
        rv[name] = GroupingComponent(
            id="chained-exception",
            values=component_list,
            tree_label=calculate_tree_label(reversed(component_list)),
        )

    return rv
Beispiel #8
0
 def interface(self):
     return Exception.to_python(
         dict(values=[{
             'type': 'ValueError',
             'value': 'hello world',
             'module': 'foo.bar',
             'stacktrace': {
                 'frames': [{
                     'filename': 'foo/baz.py',
                     'lineno': 1,
                     'in_app': True,
                 }]
             },
         }, {
             'type': 'ValueError',
             'value': 'hello world',
             'module': 'foo.bar',
             'stacktrace': {
                 'frames': [{
                     'filename': 'foo/baz.py',
                     'lineno': 1,
                     'in_app': True,
                 }]
             },
         }]))
Beispiel #9
0
 def test_non_string_value_with_no_type(self):
     inst = Exception.to_python(
         {
             'value': {'foo': 'bar'},
         }
     )
     assert inst.values[0].value == '{"foo":"bar"}'
Beispiel #10
0
 def test_get_composite_hash_uses_exception_value_if_no_type_or_stack(self):
     interface = Stacktrace(frames=[])
     interface_exc = Exception.to_python(dict(value='bar'))
     result = interface.get_composite_hash({
         'sentry.interfaces.Exception': interface_exc,
     })
     self.assertEquals(result[-1], 'bar')
Beispiel #11
0
 def interface(self):
     return Exception.to_python(
         dict(
             values=[
                 {
                     'type': 'ValueError',
                     'value': 'hello world',
                     'module': 'foo.bar',
                     'stacktrace': {
                         'frames': [{
                             'filename': 'foo/baz.py',
                             'lineno': 1,
                             'in_app': True,
                         }]
                     },
                 }, {
                     'type': 'ValueError',
                     'value': 'hello world',
                     'module': 'foo.bar',
                     'stacktrace': {
                         'frames': [{
                             'filename': 'foo/baz.py',
                             'lineno': 1,
                             'in_app': True,
                         }]
                     },
                 }
             ]
         )
     )
Beispiel #12
0
    def test_context_with_symbols(self):
        inst = Exception.to_python(
            dict(
                values=[
                    {
                        'type': 'ValueError',
                        'value': 'hello world',
                        'module': 'foo.bar',
                        'stacktrace': {
                            'frames': [
                                {
                                    'filename': 'foo/baz.py',
                                    'function': 'myfunc',
                                    'symbol': 'Class.myfunc',
                                    'lineno': 1,
                                    'in_app': True,
                                }
                            ]
                        },
                    }
                ]
            )
        )

        self.create_event(data={
            'sentry.interfaces.Exception': inst.to_json(),
        })
        context = inst.get_api_context()
        assert context['values'][0]['stacktrace']['frames'][0]['symbol'] == 'Class.myfunc'
    def test_over_max(self):
        values = []
        for x in range(5):
            exc = {'value': 'exc %d' % x, 'stacktrace': {'frames': []}}
            values.append(exc)
            for y in range(5):
                exc['stacktrace']['frames'].append({
                    'filename': 'exc %d frame %d' % (x, y),
                    'vars': {'foo': 'bar'},
                    'context_line': 'b',
                    'pre_context': ['a'],
                    'post_context': ['c'],
                })

        interface = Exception.to_python({'values': values})

        # slim to 10 frames to make tests easier
        slim_exception_data(interface, 10)

        assert len(interface.values) == 5
        for e_num, value in enumerate(interface.values):
            assert value.value == 'exc %d' % e_num
            assert len(value.stacktrace.frames) == 5
            for f_num, frame in enumerate(value.stacktrace.frames):
                assert frame.filename == 'exc %d frame %d' % (e_num, f_num)
                if e_num in (0, 4):
                    assert frame.vars is not None
                    assert frame.pre_context is not None
                    assert frame.post_context is not None
                else:
                    assert frame.vars is None
                    assert frame.pre_context is None
                    assert frame.post_context is None
Beispiel #14
0
    def test_context_with_only_app_frames(self):
        values = [{
            'type': 'ValueError',
            'value': 'hello world',
            'module': 'foo.bar',
            'stacktrace': {
                'frames': [{
                    'filename': 'foo/baz.py',
                    'lineno': 1,
                    'in_app': True,
                }]
            },
        }, {
            'type': 'ValueError',
            'value': 'hello world',
            'module': 'foo.bar',
            'stacktrace': {
                'frames': [{
                    'filename': 'foo/baz.py',
                    'lineno': 1,
                    'in_app': True,
                }]
            },
        }]
        exc = dict(values=values)
        normalize_in_app({'exception': exc})
        inst = Exception.to_python(exc)

        self.create_event(data={
            'exception': inst.to_json(),
        })
        context = inst.get_api_context()
        assert not context['hasSystemFrames']
Beispiel #15
0
 def test_non_string_value_with_no_type(self):
     inst = Exception.to_python({
         'value': {
             'foo': 'bar'
         },
     })
     assert inst.values[0].value == '{"foo":"bar"}'
Beispiel #16
0
    def test_context_with_mixed_frames(self):
        inst = Exception.to_python(
            dict(values=[{
                'type': 'ValueError',
                'value': 'hello world',
                'module': 'foo.bar',
                'stacktrace': {
                    'frames': [{
                        'filename': 'foo/baz.py',
                        'lineno': 1,
                        'in_app': True,
                    }]
                },
            }, {
                'type': 'ValueError',
                'value': 'hello world',
                'module': 'foo.bar',
                'stacktrace': {
                    'frames': [{
                        'filename': 'foo/baz.py',
                        'lineno': 1,
                        'in_app': False,
                    }]
                },
            }]))

        event = self.create_event(
            data={
                'sentry.interfaces.Exception': inst.to_json(),
            })
        context = inst.get_context(event)
        assert context['system_frames'] == 1
        assert context['exceptions'][0]['stacktrace']['system_frames'] == 0
        assert context['exceptions'][1]['stacktrace']['system_frames'] == 1
Beispiel #17
0
    def test_context_with_raw_stacks(self):
        inst = Exception.to_python(
            dict(values=[{
                'type': 'ValueError',
                'value': 'hello world',
                'module': 'foobar',
                'raw_stacktrace': {
                    'frames': [{
                        'filename': None,
                        'lineno': 1,
                        'function': '<redacted>',
                        'in_app': True,
                    }]
                },
                'stacktrace': {
                    'frames': [{
                        'filename': 'foo/baz.c',
                        'lineno': 1,
                        'function': 'main',
                        'in_app': True,
                    }]
                },
            }]))

        self.create_event(data={
            'exception': inst.to_json(),
        })
        context = inst.get_api_context()
        assert context['values'][0]['stacktrace']['frames'][0][
            'function'] == 'main'
        assert context['values'][0]['rawStacktrace']['frames'][0][
            'function'] == '<redacted>'
Beispiel #18
0
    def test_over_max(self):
        values = []
        for x in range(5):
            exc = {'value': 'exc %d' % x, 'stacktrace': {'frames': []}}
            values.append(exc)
            for y in range(5):
                exc['stacktrace']['frames'].append({
                    'filename': 'exc %d frame %d' % (x, y),
                    'vars': {'foo': 'bar'},
                    'context_line': 'b',
                    'pre_context': ['a'],
                    'post_context': ['c'],
                })

        interface = Exception.to_python({'values': values})

        # slim to 10 frames to make tests easier
        slim_exception_data(interface, 10)

        assert len(interface.values) == 5
        for e_num, value in enumerate(interface.values):
            assert value.value == 'exc %d' % e_num
            assert len(value.stacktrace.frames) == 5
            for f_num, frame in enumerate(value.stacktrace.frames):
                assert frame.filename == 'exc %d frame %d' % (e_num, f_num)
                if e_num in (0, 4):
                    assert frame.vars is not None
                    assert frame.pre_context is not None
                    assert frame.post_context is not None
                else:
                    assert frame.vars is None
                    assert frame.pre_context is None
                    assert frame.post_context is None
Beispiel #19
0
def test_iteration():
    inst = Exception.to_python({"values": [None, {"type": "ValueError"}, None]})

    assert len(inst) == 1
    assert inst[0].type == "ValueError"
    for exc in inst:
        assert exc.type == "ValueError"
 def test_under_max(self):
     interface = Exception.to_python({'values': [
         {'value': 'foo',
          'stacktrace': {'frames': [{'filename': 'foo'}]},
         }
     ]})
     slim_exception_data(interface)
     assert len(interface.values[0].stacktrace.frames) == 1
Beispiel #21
0
 def test_under_max(self):
     interface = Exception.to_python({'values': [
         {'value': 'foo',
          'stacktrace': {'frames': [{'filename': 'foo'}]},
         }
     ]})
     slim_exception_data(interface)
     assert len(interface.values[0].stacktrace.frames) == 1
Beispiel #22
0
 def test_does_not_wrap_if_exception_omitted_present(self):
     input = {
         "exc_omitted": None,
         "mechanism": {
             "handled": True, "type": "generic"
         }
     }
     assert Exception.to_python(input).to_json() == {}
Beispiel #23
0
    def test_iteration(self):
        inst = Exception.to_python({
            'values': [None, {'type': 'ValueError'}, None]
        })

        assert len(inst) == 1
        assert inst[0].type == 'ValueError'
        for exc in inst:
            assert exc.type == 'ValueError'
Beispiel #24
0
def test_iteration():
    inst = Exception.to_python({
        'values': [None, {'type': 'ValueError'}, None]
    })

    assert len(inst) == 1
    assert inst[0].type == 'ValueError'
    for exc in inst:
        assert exc.type == 'ValueError'
 def test_args_as_keyword_args(self):
     inst = Exception.to_python(dict(values=[{
         'type': 'ValueError',
         'value': 'hello world',
         'module': 'foo.bar',
     }]))
     assert type(inst.values[0]) is SingleException
     assert inst.values[0].type == 'ValueError'
     assert inst.values[0].value == 'hello world'
     assert inst.values[0].module == 'foo.bar'
Beispiel #26
0
 def test_args_as_keyword_args(self):
     inst = Exception.to_python(dict(values=[{
         'type': 'ValueError',
         'value': 'hello world',
         'module': 'foo.bar',
     }]))
     assert type(inst.values[0]) is SingleException
     assert inst.values[0].type == 'ValueError'
     assert inst.values[0].value == 'hello world'
     assert inst.values[0].module == 'foo.bar'
Beispiel #27
0
 def test_args_as_old_style(self):
     inst = Exception.to_python({
         'type': 'ValueError',
         'value': 'hello world',
         'module': 'foo.bar',
     })
     assert type(inst.values[0]) is SingleException
     assert inst.values[0].type == 'ValueError'
     assert inst.values[0].value == 'hello world'
     assert inst.values[0].module == 'foo.bar'
Beispiel #28
0
 def test_args_as_old_style(self):
     inst = Exception.to_python({
         'type': 'ValueError',
         'value': 'hello world',
         'module': 'foo.bar',
     })
     assert isinstance(inst.values[0], SingleException)
     assert inst.values[0].type == 'ValueError'
     assert inst.values[0].value == 'hello world'
     assert inst.values[0].module == 'foo.bar'
Beispiel #29
0
 def test_get_composite_hash_uses_exception_if_present(self):
     interface = Stacktrace.to_python(dict(frames=[{
         'context_line': 'foo bar',
         'lineno': 1,
         'filename': 'foo.py',
         'function': 'bar'
     }]))
     interface_exc = Exception.to_python(dict(type='exception', value='bar'))
     result = interface.get_composite_hash({
         'sentry.interfaces.Exception': interface_exc,
     })
     self.assertEquals(result[-1], 'exception')
Beispiel #30
0
def test_slim_exception_data_under_max(insta_snapshot):
    interface = Exception.to_python({
        'values': [{
            'value': 'foo',
            'stacktrace': {
                'frames': [{
                    'filename': 'foo'
                }]
            },
        }]
    })
    slim_exception_data(interface)
    insta_snapshot(interface.to_json())
Beispiel #31
0
def test_slim_exception_data_under_max(insta_snapshot):
    interface = Exception.to_python({
        "values": [{
            "value": "foo",
            "stacktrace": {
                "frames": [{
                    "filename": "foo"
                }]
            }
        }]
    })
    slim_exception_data(interface)
    insta_snapshot(interface.to_json())
Beispiel #32
0
def test_slim_exception_data_under_max(insta_snapshot):
    interface = Exception.to_python(
        {
            'values': [{
                'value': 'foo',
                'stacktrace': {
                    'frames': [{
                        'filename': 'foo'
                    }]
                },
            }]
        }
    )
    slim_exception_data(interface)
    insta_snapshot(interface.to_json())
Beispiel #33
0
 def interface(self):
     return Exception.to_python(
         dict(
             values=[
                 {
                     "type": "ValueError",
                     "value": "hello world",
                     "module": "foo.bar",
                     "stacktrace": {"frames": [{"filename": "foo/baz.py", "lineno": 1, "in_app": True}]},
                 },
                 {
                     "type": "ValueError",
                     "value": "hello world",
                     "module": "foo.bar",
                     "stacktrace": {"frames": [{"filename": "foo/baz.py", "lineno": 1, "in_app": True}]},
                 },
             ]
         )
     )
Beispiel #34
0
def test_slim_exception_data_over_max(insta_snapshot):
    values = []
    for x in range(5):
        exc = {'value': 'exc %d' % x, 'stacktrace': {'frames': []}}
        values.append(exc)
        for y in range(5):
            exc['stacktrace']['frames'].append(
                {
                    'filename': 'exc %d frame %d' % (x, y),
                    'vars': {
                        'foo': 'bar'
                    },
                    'context_line': 'b',
                    'pre_context': ['a'],
                    'post_context': ['c'],
                }
            )

    interface = Exception.to_python({'values': values})
    # slim to 10 frames to make tests easier
    slim_exception_data(interface, 10)
    insta_snapshot(interface.to_json())
Beispiel #35
0
def test_slim_exception_data_over_max(insta_snapshot):
    values = []
    for x in range(5):
        exc = {'value': 'exc %d' % x, 'stacktrace': {'frames': []}}
        values.append(exc)
        for y in range(5):
            exc['stacktrace']['frames'].append({
                'filename':
                'exc %d frame %d' % (x, y),
                'vars': {
                    'foo': 'bar'
                },
                'context_line':
                'b',
                'pre_context': ['a'],
                'post_context': ['c'],
            })

    interface = Exception.to_python({'values': values})
    # slim to 10 frames to make tests easier
    slim_exception_data(interface, 10)
    insta_snapshot(interface.to_json())
Beispiel #36
0
def test_slim_exception_data_over_max(insta_snapshot):
    values = []
    for x in range(5):
        exc = {"value": "exc %d" % x, "stacktrace": {"frames": []}}
        values.append(exc)
        for y in range(5):
            exc["stacktrace"]["frames"].append({
                "filename":
                "exc %d frame %d" % (x, y),
                "vars": {
                    "foo": "bar"
                },
                "context_line":
                "b",
                "pre_context": ["a"],
                "post_context": ["c"],
            })

    interface = Exception.to_python({"values": values})
    # slim to 10 frames to make tests easier
    slim_exception_data(interface, 10)
    insta_snapshot(interface.to_json())
Beispiel #37
0
    def test_context_with_only_system_frames(self):
        inst = Exception.to_python(
            dict(
                values=[
                    {
                        "type": "ValueError",
                        "value": "hello world",
                        "module": "foo.bar",
                        "stacktrace": {"frames": [{"filename": "foo/baz.py", "lineno": 1, "in_app": False}]},
                    },
                    {
                        "type": "ValueError",
                        "value": "hello world",
                        "module": "foo.bar",
                        "stacktrace": {"frames": [{"filename": "foo/baz.py", "lineno": 1, "in_app": False}]},
                    },
                ]
            )
        )

        self.create_event(data={"sentry.interfaces.Exception": inst.to_json()})
        context = inst.get_api_context()
        assert not context["hasSystemFrames"]
    def test_over_max(self):
        values = []
        for x in xrange(5):
            exc = {"value": "exc %d" % x, "stacktrace": {"frames": []}}
            values.append(exc)
            for y in xrange(5):
                exc["stacktrace"]["frames"].append(
                    {
                        "filename": "exc %d frame %d" % (x, y),
                        "vars": {"foo": "bar"},
                        "context_line": "b",
                        "pre_context": ["a"],
                        "post_context": ["c"],
                    }
                )

        interface = Exception.to_python({"values": values})

        # slim to 10 frames to make tests easier
        slim_exception_data(interface, 10)

        assert len(interface.values) == 5
        for e_num, value in enumerate(interface.values):
            assert value.value == "exc %d" % e_num
            assert len(value.stacktrace.frames) == 5
            for f_num, frame in enumerate(value.stacktrace.frames):
                assert frame.filename == "exc %d frame %d" % (e_num, f_num)
                print (frame.filename)
                if e_num in (0, 4):
                    assert frame.vars is not None
                    assert frame.pre_context is not None
                    assert frame.post_context is not None
                else:
                    assert frame.vars is None
                    assert frame.pre_context is None
                    assert frame.post_context is None
Beispiel #39
0
def chained_exception_legacy(interface: ChainedException, event: Event,
                             context: GroupingContext,
                             **meta: Any) -> ReturnedVariants:
    # Case 1: we have a single exception, use the single exception
    # component directly
    exceptions = interface.exceptions()
    if len(exceptions) == 1:
        single_variant: GroupingComponent = context.get_grouping_component(
            exceptions[0], event=event, **meta)
        return {context["variant"]: single_variant}

    # Case 2: try to build a new component out of the individual
    # errors however with a trick.  In case any exception has a
    # stacktrace we want to ignore all other exceptions.
    any_stacktraces = False
    values = []
    for exception in exceptions:
        exception_component: GroupingComponent = context.get_grouping_component(
            exception, event=event, **meta)
        stacktrace_component = exception_component.get_subcomponent(
            "stacktrace")
        if stacktrace_component is not None and stacktrace_component.contributes:
            any_stacktraces = True
        values.append(exception_component)

    if any_stacktraces:
        for value in values:
            stacktrace_component = value.get_subcomponent("stacktrace")
            if stacktrace_component is None or not stacktrace_component.contributes:
                value.update(contributes=False,
                             hint="exception has no stacktrace")

    return {
        context["variant"]:
        GroupingComponent(id="chained-exception", values=values)
    }
 def test_under_max(self):
     interface = Exception.to_python({"values": [{"value": "foo", "stacktrace": {"frames": [{"filename": "foo"}]}}]})
     slim_exception_data(interface)
     assert len(interface.values[0].stacktrace.frames) == 1
Beispiel #41
0
 def test_args_as_keyword_args(self):
     inst = Exception.to_python(dict(values=[{"type": "ValueError", "value": "hello world", "module": "foo.bar"}]))
     assert type(inst.values[0]) is SingleException
     assert inst.values[0].type == "ValueError"
     assert inst.values[0].value == "hello world"
     assert inst.values[0].module == "foo.bar"
Beispiel #42
0
 def test_args_as_old_style(self):
     inst = Exception.to_python({"type": "ValueError", "value": "hello world", "module": "foo.bar"})
     assert type(inst.values[0]) is SingleException
     assert inst.values[0].type == "ValueError"
     assert inst.values[0].value == "hello world"
     assert inst.values[0].module == "foo.bar"