Skip to content

Commit 0097e60

Browse files
authored
bring back manual exception printing (#5823)
* bring back manual exception printing * dang it darglint
1 parent 4bc4cb8 commit 0097e60

File tree

2 files changed

+87
-9
lines changed

2 files changed

+87
-9
lines changed

reflex/reflex.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,12 @@ def _run(
216216
*args,
217217
**kwargs,
218218
)
219-
compile_future.result()
219+
return_result = compile_future.result()
220220
else:
221-
app_task(*args, **kwargs)
221+
return_result = app_task(*args, **kwargs)
222+
223+
if not return_result:
224+
raise SystemExit(1)
222225

223226
# Get the frontend and backend commands, based on the environment.
224227
setup_frontend = frontend_cmd = backend_cmd = None

reflex/utils/prerequisites.py

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,25 +269,100 @@ def get_compiled_app(
269269
return app_module
270270

271271

272+
def _can_colorize() -> bool:
273+
"""Check if the output can be colorized.
274+
275+
Copied from _colorize.can_colorize.
276+
277+
https://raw.githubusercontent.com/python/cpython/refs/heads/main/Lib/_colorize.py
278+
279+
Returns:
280+
If the output can be colorized
281+
"""
282+
import io
283+
import os
284+
285+
def _safe_getenv(k: str, fallback: str | None = None) -> str | None:
286+
"""Exception-safe environment retrieval. See gh-128636.
287+
288+
Args:
289+
k: The environment variable key.
290+
fallback: The fallback value if the environment variable is not set.
291+
292+
Returns:
293+
The value of the environment variable or the fallback value.
294+
"""
295+
try:
296+
return os.environ.get(k, fallback)
297+
except Exception:
298+
return fallback
299+
300+
file = sys.stdout
301+
302+
if not sys.flags.ignore_environment:
303+
if _safe_getenv("PYTHON_COLORS") == "0":
304+
return False
305+
if _safe_getenv("PYTHON_COLORS") == "1":
306+
return True
307+
if _safe_getenv("NO_COLOR"):
308+
return False
309+
if _safe_getenv("FORCE_COLOR"):
310+
return True
311+
if _safe_getenv("TERM") == "dumb":
312+
return False
313+
314+
if not hasattr(file, "fileno"):
315+
return False
316+
317+
if sys.platform == "win32":
318+
try:
319+
import nt
320+
321+
if not nt._supports_virtual_terminal():
322+
return False
323+
except (ImportError, AttributeError):
324+
return False
325+
326+
try:
327+
return os.isatty(file.fileno())
328+
except io.UnsupportedOperation:
329+
return hasattr(file, "isatty") and file.isatty()
330+
331+
272332
def compile_or_validate_app(
273333
compile: bool = False,
274334
check_if_schema_up_to_date: bool = False,
275335
prerender_routes: bool = False,
276-
):
336+
) -> bool:
277337
"""Compile or validate the app module based on the default config.
278338
279339
Args:
280340
compile: Whether to compile the app.
281341
check_if_schema_up_to_date: If True, check if the schema is up to date.
282342
prerender_routes: Whether to prerender routes.
343+
344+
Returns:
345+
True if the app was successfully compiled or validated, False otherwise.
283346
"""
284-
if compile:
285-
get_compiled_app(
286-
check_if_schema_up_to_date=check_if_schema_up_to_date,
287-
prerender_routes=prerender_routes,
288-
)
347+
try:
348+
if compile:
349+
get_compiled_app(
350+
check_if_schema_up_to_date=check_if_schema_up_to_date,
351+
prerender_routes=prerender_routes,
352+
)
353+
else:
354+
get_and_validate_app(check_if_schema_up_to_date=check_if_schema_up_to_date)
355+
except Exception as e:
356+
import traceback
357+
358+
try:
359+
colorize = _can_colorize()
360+
traceback.print_exception(e, colorize=colorize) # pyright: ignore[reportCallIssue]
361+
except Exception:
362+
traceback.print_exception(e)
363+
return False
289364
else:
290-
get_and_validate_app(check_if_schema_up_to_date=check_if_schema_up_to_date)
365+
return True
291366

292367

293368
def get_redis() -> Redis | None:

0 commit comments

Comments
 (0)