Skip to content

Commit f226bb8

Browse files
authored
ENG-7619: make console errors go to stderr (#5789)
* ENG-7619: make console errors go to stderr * check err not out
1 parent 7830671 commit f226bb8

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

reflex/utils/console.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
# Console for pretty printing.
2424
_console = Console()
25+
_console_stderr = Console(stderr=True)
2526

2627
# The current log level.
2728
_LOG_LEVEL = LogLevel.INFO
@@ -96,6 +97,21 @@ def print(msg: str, *, dedupe: bool = False, **kwargs):
9697
_console.print(msg, **kwargs)
9798

9899

100+
def _print_stderr(msg: str, *, dedupe: bool = False, **kwargs):
101+
"""Print a message to stderr.
102+
103+
Args:
104+
msg: The message to print.
105+
dedupe: If True, suppress multiple console logs of print message.
106+
kwargs: Keyword arguments to pass to the print function.
107+
"""
108+
if dedupe:
109+
if msg in _EMITTED_PRINTS:
110+
return
111+
_EMITTED_PRINTS.add(msg)
112+
_console_stderr.print(msg, **kwargs)
113+
114+
99115
@once
100116
def log_file_console():
101117
"""Create a console that logs to a file.
@@ -342,7 +358,7 @@ def error(msg: str, *, dedupe: bool = False, **kwargs):
342358
if msg in _EMITTED_ERRORS:
343359
return
344360
_EMITTED_ERRORS.add(msg)
345-
print(f"[red]{msg}[/red]", **kwargs)
361+
_print_stderr(f"[red]{msg}[/red]", **kwargs)
346362
if should_use_log_file_console():
347363
print_to_log_file(f"[red]{msg}[/red]", **kwargs)
348364

reflex/vars/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,7 @@ def __get__(self, instance: BaseState | None, owner: type):
23452345
def _check_deprecated_return_type(self, instance: BaseState, value: Any) -> None:
23462346
if not _isinstance(value, self._var_type, nested=1, treat_var_as_type=False):
23472347
console.error(
2348-
f"Computed var '{type(instance).__name__}.{self._js_expr}' must return"
2348+
f"Computed var '{type(instance).__name__}.{self._name}' must return"
23492349
f" a value of type '{escape(str(self._var_type))}', got '{value!s}' of type {type(value)}."
23502350
)
23512351

@@ -2395,7 +2395,7 @@ def _deps(
23952395
except Exception as e:
23962396
console.warn(
23972397
"Failed to automatically determine dependencies for computed var "
2398-
f"{objclass.__name__}.{self._js_expr}: {e}. "
2398+
f"{objclass.__name__}.{self._name}: {e}. "
23992399
"Provide static_deps and set auto_deps=False to suppress this warning."
24002400
)
24012401
return d

tests/integration/test_exception_handlers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def driver(test_app: AppHarness) -> Generator[WebDriver, None, None]:
114114

115115
def test_frontend_exception_handler_during_runtime(
116116
driver: WebDriver,
117-
capsys,
117+
capsys: pytest.CaptureFixture[str],
118118
):
119119
"""Test calling frontend exception handler during runtime.
120120
@@ -136,13 +136,13 @@ def test_frontend_exception_handler_during_runtime(
136136
time.sleep(2)
137137

138138
captured_default_handler_output = capsys.readouterr()
139-
assert "induce_frontend_error" in captured_default_handler_output.out
140-
assert "ReferenceError" in captured_default_handler_output.out
139+
assert "induce_frontend_error" in captured_default_handler_output.err
140+
assert "ReferenceError" in captured_default_handler_output.err
141141

142142

143143
def test_backend_exception_handler_during_runtime(
144144
driver: WebDriver,
145-
capsys,
145+
capsys: pytest.CaptureFixture[str],
146146
):
147147
"""Test calling backend exception handler during runtime.
148148
@@ -164,14 +164,14 @@ def test_backend_exception_handler_during_runtime(
164164
time.sleep(2)
165165

166166
captured_default_handler_output = capsys.readouterr()
167-
assert "divide_by_number" in captured_default_handler_output.out
168-
assert "ZeroDivisionError" in captured_default_handler_output.out
167+
assert "divide_by_number" in captured_default_handler_output.err
168+
assert "ZeroDivisionError" in captured_default_handler_output.err
169169

170170

171171
def test_frontend_exception_handler_with_react(
172172
test_app: AppHarness,
173173
driver: WebDriver,
174-
capsys,
174+
capsys: pytest.CaptureFixture[str],
175175
):
176176
"""Test calling frontend exception handler during runtime.
177177
@@ -194,9 +194,9 @@ def test_frontend_exception_handler_with_react(
194194

195195
captured_default_handler_output = capsys.readouterr()
196196
if isinstance(test_app, AppHarnessProd):
197-
assert "Error: Minified React error #31" in captured_default_handler_output.out
197+
assert "Error: Minified React error #31" in captured_default_handler_output.err
198198
else:
199199
assert (
200200
"Error: Objects are not valid as a React child (found: object with keys \n{invalid})"
201-
in captured_default_handler_output.out
201+
in captured_default_handler_output.err
202202
)

tests/units/test_state.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,7 @@ def reset(self):
16251625

16261626

16271627
@pytest.mark.asyncio
1628-
async def test_state_with_invalid_yield(capsys, mock_app):
1628+
async def test_state_with_invalid_yield(capsys: pytest.CaptureFixture[str], mock_app):
16291629
"""Test that an error is thrown when a state yields an invalid value.
16301630
16311631
Args:
@@ -1664,7 +1664,7 @@ def invalid_handler(self):
16641664
token="",
16651665
)
16661666
captured = capsys.readouterr()
1667-
assert "must only return/yield: None, Events or other EventHandlers" in captured.out
1667+
assert "must only return/yield: None, Events or other EventHandlers" in captured.err
16681668

16691669

16701670
@pytest_asyncio.fixture(

0 commit comments

Comments
 (0)