def test_exc_frame_locals(self):
        def baz():
            a = 1
            b = 2
            assert a == 1
            assert b == 2
            raise NotImplementedError

        def bar():
            c = 3
            d = 4
            assert c == 3
            assert d == 4
            baz()

        def foo():
            e = 5
            f = 6
            assert e == 5
            assert f == 6
            bar()

        try:
            baz()
        except NotImplementedError:
            self.assertEqual(server_util.exc_frame_locals(), {'a': 1, 'b': 2})

        try:
            foo()
        except NotImplementedError:
            self.assertEqual(server_util.exc_frame_locals(), {'a': 1, 'b': 2})
Example #2
0
    def report_exception(self, exc):
        query = formatting.key_value_list(request.query.allitems())
        forms = formatting.key_value_list(
            self._censor_passwords(request.forms.allitems()) if request.json is None else []
        )
        body = formatting.verbose_pretty_json(request.json)
        local_vars = formatting.key_value_list(
            self._censor_passwords(server_util.exc_frame_locals().items())
        )
        aux_info = textwrap.dedent(
            """\
                    Query params:
                    {0}

                    Form params:
                    {1}

                    JSON body:
                    {2}

                    Local variables:
                    {3}"""
        ).format(query, forms, body, local_vars)

        if len(aux_info) > self.MAX_AUX_INFO_LENGTH:
            aux_info = (
                aux_info[: (self.MAX_AUX_INFO_LENGTH // 2)]
                + "(...truncated...)"
                + aux_info[-(self.MAX_AUX_INFO_LENGTH // 2) :]
            )

        message = textwrap.dedent(
            """\
             Error on request by {0.user}:

             {0.method} {0.path}

             {1}

             {2}"""
        ).format(request, aux_info, traceback.format_exc())

        # Both print to console and send email
        print(message, file=sys.stderr)
        self.send_email(exc, message)
Example #3
0
    def report_exception(self, exc):
        query = formatting.key_value_list(request.query.allitems())
        forms = formatting.key_value_list(
            self._censor_passwords(request.forms.allitems()) if request.json is None else []
        )
        body = formatting.verbose_pretty_json(request.json)
        local_vars = formatting.key_value_list(
            self._censor_passwords(server_util.exc_frame_locals().items())
        )
        aux_info = textwrap.dedent(
            """\
                    Query params:
                    {0}

                    Form params:
                    {1}

                    JSON body:
                    {2}

                    Local variables:
                    {3}"""
        ).format(query, forms, body, local_vars)

        if len(aux_info) > self.MAX_AUX_INFO_LENGTH:
            aux_info = (
                aux_info[: (self.MAX_AUX_INFO_LENGTH / 2)]
                + "(...truncated...)"
                + aux_info[-(self.MAX_AUX_INFO_LENGTH / 2) :]
            )

        message = textwrap.dedent(
            """\
             Error on request by {0.user}:

             {0.method} {0.path}

             {1}

             {2}"""
        ).format(request, aux_info, traceback.format_exc())

        # Both print to console and send email
        print >>sys.stderr, message
        self.send_email(exc, message)