Skip to content

Commit f9faea7

Browse files
committed
Pretty print error messages
1 parent 9b05c59 commit f9faea7

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/semiwrap/hooks.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# TODO: rename this
22

3+
import os
34
import pathlib
5+
import sys
6+
import traceback
47
import typing as T
58

69
from hatchling.plugin import hookimpl
@@ -52,9 +55,45 @@ def initialize(self, version: str, build_data: T.Dict[str, T.Any]) -> None:
5255
trampoline_build_path = stage0_build_path / "trampolines"
5356
trampoline_meson_build = trampoline_build_path / "meson.build"
5457

55-
eps = gen_meson_to_file(
56-
project_root, stage0_meson_build, stage1_meson_build, trampoline_meson_build
57-
)
58+
try:
59+
eps = gen_meson_to_file(
60+
project_root,
61+
stage0_meson_build,
62+
stage1_meson_build,
63+
trampoline_meson_build,
64+
)
65+
except Exception as e:
66+
# Reading the stack trace is annoying, most of the time the exception content
67+
# is enough to figure out what you did wrong.
68+
if os.environ.get("SEMIWRAP_ERROR_VERBOSE") == "1":
69+
raise
70+
71+
msg = [
72+
"ERROR: exception occurred when processing `pyproject.toml`",
73+
"",
74+
]
75+
76+
msg += traceback.format_exception_only(e, show_group=True)
77+
cause = e.__context__
78+
while cause is not None:
79+
if "prepare_metadata_for_build_editable" in str(cause):
80+
break
81+
82+
el = traceback.format_exception_only(cause, show_group=True)
83+
el[0] = f"- caused by {el[0]}"
84+
msg += el
85+
86+
if cause.__suppress_context__:
87+
break
88+
89+
cause = cause.__context__
90+
91+
msg.append(
92+
"Set environment variable SEMIWRAP_ERROR_VERBOSE=1 for stacktrace"
93+
)
94+
95+
print("\n".join(msg), file=sys.stderr)
96+
sys.exit(1)
5897

5998
if eps:
6099
# .. not documented but it works?

0 commit comments

Comments
 (0)