mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 18:18:25 +00:00
User script exception handler[squash]
fixing 2837, added test unified-function deleting the wrong commit
This commit is contained in:
parent
c6802ba034
commit
b8fbe71c93
@ -5,6 +5,7 @@ import time
|
||||
import sys
|
||||
import types
|
||||
import typing
|
||||
import traceback
|
||||
|
||||
from mitmproxy import addonmanager
|
||||
from mitmproxy import exceptions
|
||||
@ -36,6 +37,25 @@ def load_script(path: str) -> types.ModuleType:
|
||||
sys.path[:] = oldpath
|
||||
|
||||
|
||||
def script_error_handler(path, exc, msg="", tb=False):
|
||||
"""
|
||||
Handles all the user's script errors with
|
||||
an optional traceback
|
||||
"""
|
||||
exception = type(exc).__name__
|
||||
if msg:
|
||||
exception = msg
|
||||
lineno = ""
|
||||
if hasattr(exc, "lineno"):
|
||||
lineno = str(exc.lineno)
|
||||
log_msg = "Error in Script {}:{} {}".format(path, lineno, exception)
|
||||
if tb:
|
||||
etype, value, tback = sys.exc_info()
|
||||
tback = addonmanager.cut_traceback(tback, "invoke_addon")
|
||||
log_msg = log_msg.join(["\n"] + traceback.format_exception(etype, value, tback))
|
||||
ctx.log.error(log_msg)
|
||||
|
||||
|
||||
class Script:
|
||||
"""
|
||||
An addon that manages a single script.
|
||||
|
@ -243,6 +243,18 @@ class TestScriptLoader:
|
||||
tctx.invoke(sc, "tick")
|
||||
assert len(tctx.master.addons) == 1
|
||||
|
||||
def test_script_error_handler(self):
|
||||
path = "/sample/path/example.py"
|
||||
exc = SyntaxError
|
||||
msg = "Error raised"
|
||||
tb = True
|
||||
with taddons.context() as tctx:
|
||||
script.script_error_handler(path, exc, msg, tb)
|
||||
assert tctx.master.has_log("/sample/path/example.py")
|
||||
assert tctx.master.has_log("Error raised")
|
||||
assert tctx.master.has_log("lineno")
|
||||
assert tctx.master.has_log("NoneType")
|
||||
|
||||
def test_order(self):
|
||||
rec = tutils.test_data.path("mitmproxy/data/addonscripts/recorder")
|
||||
sc = script.ScriptLoader()
|
||||
|
Loading…
Reference in New Issue
Block a user