mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-02-02 00:05:27 +00:00
Handling user script exceptions, fix #2839
This commit is contained in:
parent
5e2a1ec23c
commit
6780e5025a
@ -25,6 +25,7 @@ def load_script(path: str) -> types.ModuleType:
|
|||||||
sys.modules.pop(fullname, None)
|
sys.modules.pop(fullname, None)
|
||||||
oldpath = sys.path
|
oldpath = sys.path
|
||||||
sys.path.insert(0, os.path.dirname(path))
|
sys.path.insert(0, os.path.dirname(path))
|
||||||
|
m = None
|
||||||
try:
|
try:
|
||||||
loader = importlib.machinery.SourceFileLoader(fullname, path)
|
loader = importlib.machinery.SourceFileLoader(fullname, path)
|
||||||
spec = importlib.util.spec_from_loader(fullname, loader=loader)
|
spec = importlib.util.spec_from_loader(fullname, loader=loader)
|
||||||
@ -32,9 +33,11 @@ def load_script(path: str) -> types.ModuleType:
|
|||||||
loader.exec_module(m)
|
loader.exec_module(m)
|
||||||
if not getattr(m, "name", None):
|
if not getattr(m, "name", None):
|
||||||
m.name = path # type: ignore
|
m.name = path # type: ignore
|
||||||
return m
|
except Exception as e:
|
||||||
|
script_error_handler(path, e, msg=str(e))
|
||||||
finally:
|
finally:
|
||||||
sys.path[:] = oldpath
|
sys.path[:] = oldpath
|
||||||
|
return m
|
||||||
|
|
||||||
|
|
||||||
def script_error_handler(path, exc, msg="", tb=False):
|
def script_error_handler(path, exc, msg="", tb=False):
|
||||||
@ -48,11 +51,11 @@ def script_error_handler(path, exc, msg="", tb=False):
|
|||||||
lineno = ""
|
lineno = ""
|
||||||
if hasattr(exc, "lineno"):
|
if hasattr(exc, "lineno"):
|
||||||
lineno = str(exc.lineno)
|
lineno = str(exc.lineno)
|
||||||
log_msg = "in Script {}:{} {}".format(path, lineno, exception)
|
log_msg = "in script {}:{} {}".format(path, lineno, exception)
|
||||||
if tb:
|
if tb:
|
||||||
etype, value, tback = sys.exc_info()
|
etype, value, tback = sys.exc_info()
|
||||||
tback = addonmanager.cut_traceback(tback, "invoke_addon")
|
tback = addonmanager.cut_traceback(tback, "invoke_addon")
|
||||||
log_msg = log_msg.join(["\n"] + traceback.format_exception(etype, value, tback))
|
log_msg = log_msg + "\n" + "".join(traceback.format_exception(etype, value, tback))
|
||||||
ctx.log.error(log_msg)
|
ctx.log.error(log_msg)
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,7 +12,9 @@ from mitmproxy.test import tflow
|
|||||||
from mitmproxy.test import tutils
|
from mitmproxy.test import tutils
|
||||||
|
|
||||||
|
|
||||||
def test_load_script():
|
@pytest.mark.asyncio
|
||||||
|
async def test_load_script():
|
||||||
|
with taddons.context() as tctx:
|
||||||
ns = script.load_script(
|
ns = script.load_script(
|
||||||
tutils.test_data.path(
|
tutils.test_data.path(
|
||||||
"mitmproxy/data/addonscripts/recorder/recorder.py"
|
"mitmproxy/data/addonscripts/recorder/recorder.py"
|
||||||
@ -20,10 +22,17 @@ def test_load_script():
|
|||||||
)
|
)
|
||||||
assert ns.addons
|
assert ns.addons
|
||||||
|
|
||||||
with pytest.raises(FileNotFoundError):
|
|
||||||
script.load_script(
|
script.load_script(
|
||||||
"nonexistent"
|
"nonexistent"
|
||||||
)
|
)
|
||||||
|
assert await tctx.master.await_log("No such file or directory")
|
||||||
|
|
||||||
|
script.load_script(
|
||||||
|
tutils.test_data.path(
|
||||||
|
"mitmproxy/data/addonscripts/recorder/error.py"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert await tctx.master.await_log("invalid syntax")
|
||||||
|
|
||||||
|
|
||||||
def test_load_fullname():
|
def test_load_fullname():
|
||||||
|
7
test/mitmproxy/data/addonscripts/recorder/error.py
Normal file
7
test/mitmproxy/data/addonscripts/recorder/error.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
"""
|
||||||
|
This file is intended to have syntax errors for test purposes
|
||||||
|
"""
|
||||||
|
|
||||||
|
impotr recorder # Intended Syntax Error
|
||||||
|
|
||||||
|
addons = [recorder.Recorder("e")]
|
Loading…
Reference in New Issue
Block a user