Skip to content

Commit

Permalink
drive-by-improvement: prettify exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsteers committed Sep 1, 2024
1 parent 22af3d4 commit a00d7e9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
14 changes: 12 additions & 2 deletions airbyte/_connector_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ def check(self) -> None:
except exc.AirbyteConnectorFailedError as ex:
raise exc.AirbyteConnectorCheckFailedError(
connector_name=self.name,
log_text=ex.log_text,
) from ex
original_exception=ex,
) from None

def install(self) -> None:
"""Install the connector if it is not yet installed."""
Expand Down Expand Up @@ -373,7 +373,17 @@ def _execute(
# This is likely a log message, so log it as INFO.
self._print_info_message(line)

except exc.AirbyteSubprocessFailedError as ex:
# Generic subprocess failure, so raise a connector error.
raise exc.AirbyteConnectorFailedError(
connector_name=self.name,
log_text=ex.log_text,
context={
"exit_code": ex.exit_code,
},
) from None
except Exception as e:
# This is an unexpected error, so wrap the original exception when raising.
raise exc.AirbyteConnectorFailedError(
connector_name=self.name,
log_text=self._last_log_messages,
Expand Down
12 changes: 7 additions & 5 deletions airbyte/_executors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ def _stream_from_subprocess(
raise exc.AirbyteSubprocessFailedError(
run_args=args,
exit_code=exit_code,
context={
"exception": str(exception_holder.exception)
if exception_holder.exception
else None,
},
original_exception=(
exception_holder.exception
if not isinstance(
exception_holder.exception, exc.AirbyteConnectorBrokenPipeError
)
else None
),
)


Expand Down
35 changes: 21 additions & 14 deletions airbyte/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
DOCS_URL_BASE = "https://airbytehq.github.io/PyAirbyte"
DOCS_URL = f"{DOCS_URL_BASE}/airbyte.html"

VERTICAL_SEPARATOR = "\n" + "-" * 60


# Base error class

Expand Down Expand Up @@ -102,29 +104,32 @@ def __str__(self) -> str:
context_str = "\n ".join(
f"{str(k).replace('_', ' ').title()}: {v!r}" for k, v in display_properties.items()
)
exception_str = f"{self.__class__.__name__}: {self.get_message()}\n"
if context_str:
exception_str += " " + context_str
exception_str = (
f"{self.get_message()} ({self.__class__.__name__})"
+ VERTICAL_SEPARATOR
+ f"\n{self.__class__.__name__}: {self.get_message()}"
)

if self.log_text:
if isinstance(self.log_text, list):
self.log_text = "\n".join(self.log_text)
if self.guidance:
exception_str += f"\n {self.guidance}"

exception_str += f"\n Log output: \n {indent(self.log_text, ' ')}"
if self.help_url:
exception_str += f"\n More info: {self.help_url}"

if context_str:
exception_str += "\n " + context_str

if self.log_file:
exception_str += f"\n Log file: {self.log_file.absolute()!s}"

if self.guidance:
exception_str += f"\n Suggestion: {self.guidance}"
if self.log_text:
if isinstance(self.log_text, list):
self.log_text = "\n".join(self.log_text)

if self.help_url:
exception_str += f"\n More info: {self.help_url}"
exception_str += f"\n Log output: \n {indent(self.log_text, ' ')}"

if self.original_exception:
exception_str += (
f"\n Caused by:\n{indent(str(self.original_exception), prefix=' ')!s}"
)
exception_str += VERTICAL_SEPARATOR + f"\nCaused by: {self.original_exception!s}"

return exception_str

Expand Down Expand Up @@ -289,6 +294,8 @@ class AirbyteConnectorError(PyAirbyteError):
def __post_init__(self) -> None:
"""Set the log file path for the connector."""
self.log_file = self._get_log_file()
if not self.guidance and self.log_file:
self.guidance = "Please review the log file for more information."

def _get_log_file(self) -> Path | None:
"""Return the log file path for the connector."""
Expand Down

0 comments on commit a00d7e9

Please sign in to comment.