mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-25 09:37:37 +00:00
errorcheck: print message to stderr for console UI (#5225)
This commit is contained in:
parent
f0da667516
commit
b8bed1d770
@ -8,6 +8,8 @@
|
||||
([#5190](https://github.com/mitmproxy/mitmproxy/issues/5190), @redraw)
|
||||
* Fix a bug where the wrong SNI is sent to an upstream HTTPS proxy
|
||||
([#5109](https://github.com/mitmproxy/mitmproxy/issues/5109), @mhils)
|
||||
* Make sure that mitmproxy displays error messages on startup.
|
||||
([#5225](https://github.com/mitmproxy/mitmproxy/issues/5225), @mhils)
|
||||
|
||||
## 19 March 2022: mitmproxy 8.0.0
|
||||
|
||||
|
@ -1,15 +1,20 @@
|
||||
import asyncio
|
||||
import sys
|
||||
from typing import Optional
|
||||
|
||||
from mitmproxy import log
|
||||
|
||||
|
||||
class ErrorCheck:
|
||||
"""Monitor startup for error log entries, and terminate immediately if there are some."""
|
||||
def __init__(self):
|
||||
self.has_errored = False
|
||||
|
||||
def add_log(self, e):
|
||||
if e.level == "error":
|
||||
self.has_errored = True
|
||||
def __init__(self, log_to_stderr: bool = False):
|
||||
self.has_errored: Optional[str] = None
|
||||
self.log_to_stderr = log_to_stderr
|
||||
|
||||
def add_log(self, e: log.LogEntry):
|
||||
if not self.has_errored and e.level == "error":
|
||||
self.has_errored = e.msg
|
||||
|
||||
async def running(self):
|
||||
# don't run immediately, wait for all logging tasks to finish.
|
||||
@ -17,4 +22,6 @@ class ErrorCheck:
|
||||
|
||||
async def _shutdown_if_errored(self):
|
||||
if self.has_errored:
|
||||
if self.log_to_stderr:
|
||||
print(f"Error on startup: {self.has_errored}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
@ -56,7 +56,7 @@ class ConsoleMaster(master.Master):
|
||||
readfile.ReadFile(),
|
||||
consoleaddons.ConsoleAddon(self),
|
||||
keymap.KeymapConfig(),
|
||||
errorcheck.ErrorCheck(),
|
||||
errorcheck.ErrorCheck(log_to_stderr=True),
|
||||
)
|
||||
|
||||
self.window = None
|
||||
|
@ -6,11 +6,12 @@ from mitmproxy import log
|
||||
from mitmproxy.addons.errorcheck import ErrorCheck
|
||||
|
||||
|
||||
def test_errorcheck():
|
||||
@pytest.mark.parametrize("do_log", [True, False])
|
||||
def test_errorcheck(capsys, do_log):
|
||||
async def run():
|
||||
# suppress error that task exception was not retrieved.
|
||||
asyncio.get_running_loop().set_exception_handler(lambda *_: 0)
|
||||
e = ErrorCheck()
|
||||
e = ErrorCheck(do_log)
|
||||
e.add_log(log.LogEntry("fatal", "error"))
|
||||
await e.running()
|
||||
await asyncio.sleep(0)
|
||||
@ -18,6 +19,9 @@ def test_errorcheck():
|
||||
with pytest.raises(SystemExit):
|
||||
asyncio.run(run())
|
||||
|
||||
if do_log:
|
||||
assert capsys.readouterr().err == "Error on startup: fatal\n"
|
||||
|
||||
|
||||
async def test_no_error():
|
||||
e = ErrorCheck()
|
||||
|
Loading…
Reference in New Issue
Block a user