Skip to content

Commit fe76532

Browse files
committed
Improve formatting of ConanException
1 parent 3f865d0 commit fe76532

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/py_build_cmake/common/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import contextlib
34
import logging
45
import os
56
from dataclasses import dataclass, field
@@ -182,6 +183,13 @@ class Command:
182183

183184
def format_and_rethrow_exception(e: BaseException, component=False):
184185
"""Raises a FormattedErrorMessage from the given exception"""
186+
ConanException = None
187+
with contextlib.suppress(ImportError):
188+
from conan.errors import ( # type: ignore[import-untyped] # noqa: PLC0415
189+
ConanException as ConanException_,
190+
)
191+
192+
ConanException = ConanException_ # intermediate step to keep mypy happy
185193
if isinstance(e, FormattedErrorMessage):
186194
raise e
187195
if isinstance(e, ConfigError):
@@ -216,6 +224,15 @@ def format_and_rethrow_exception(e: BaseException, component=False):
216224
"\n\t(scroll up for subprocess output, above Python backtrace)"
217225
)
218226
raise FormattedErrorMessage(msg) from e
227+
elif ConanException is not None and isinstance(e, ConanException):
228+
logger.error("Conan failed", exc_info=False)
229+
msg = "Conan failed"
230+
msg = (
231+
f"\n\n\t\u274c {msg}:\n\n"
232+
f"\t\t{e}\n"
233+
"\n\t(scroll up for subprocess output, above Python backtrace)"
234+
)
235+
raise FormattedErrorMessage(msg) from e
219236
elif isinstance(e, AssertionError):
220237
logger.error("Internal error:", exc_info=e)
221238
msg = (

0 commit comments

Comments
 (0)