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