Skip to content

Commit

Permalink
move print logic to AirbyteTracedException.emit_message()
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroslopez committed May 6, 2022
1 parent 420c6d9 commit b00fc78
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
6 changes: 2 additions & 4 deletions airbyte-cdk/python/airbyte_cdk/exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
import sys

from airbyte_cdk.utils.airbyte_secrets_utils import filter_secrets
from airbyte_cdk.utils.traced_exception import AirbyteTracedException


Expand All @@ -29,8 +28,7 @@ def hook_fn(exception_type, exception_value, traceback_):
if issubclass(exception_type, AirbyteTracedException)
else AirbyteTracedException.from_exception(exception_value)
)
message = traced_exc.as_airbyte_message()
message_json = message.json(exclude_unset=True)
print(filter_secrets(message_json))

traced_exc.emit_message()

sys.excepthook = hook_fn
10 changes: 10 additions & 0 deletions airbyte-cdk/python/airbyte_cdk/utils/traced_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from airbyte_cdk.models import AirbyteErrorTraceMessage, AirbyteMessage, AirbyteTraceMessage, FailureType, TraceType
from airbyte_cdk.models import Type as MessageType
from airbyte_cdk.utils.airbyte_secrets_utils import filter_secrets


class AirbyteTracedException(Exception):
Expand Down Expand Up @@ -55,6 +56,15 @@ def as_airbyte_message(self) -> AirbyteMessage:

return AirbyteMessage(type=MessageType.TRACE, trace=trace_message)

def emit_message(self):
"""
Prints the exception as an AirbyteTraceMessage.
Note that this will be called automatically on uncaught exceptions when using the airbyte_cdk entrypoint.
"""
message = self.as_airbyte_message().json(exclude_unset=True)
filtered_message = filter_secrets(message)
print(filtered_message)

@classmethod
def from_exception(cls, exc: Exception, *args, **kwargs) -> "AirbyteTracedException":
"""
Expand Down
32 changes: 31 additions & 1 deletion airbyte-cdk/python/unit_tests/utils/test_traced_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# Copyright (c) 2021 Airbyte, Inc., all rights reserved.
#

import json

import pytest
from airbyte_cdk.models.airbyte_protocol import AirbyteMessage, FailureType, TraceType
from airbyte_cdk.models.airbyte_protocol import AirbyteErrorTraceMessage, AirbyteMessage, AirbyteTraceMessage, FailureType, TraceType
from airbyte_cdk.models.airbyte_protocol import Type as MessageType
from airbyte_cdk.utils.traced_exception import AirbyteTracedException

Expand Down Expand Up @@ -51,3 +53,31 @@ def test_existing_exception_as_airbyte_message(raised_exception):
assert airbyte_message.trace.error.stack_trace.endswith(
'raise RuntimeError("an error has occurred")\n' "RuntimeError: an error has occurred\n"
)


def test_emit_message(capsys):
traced_exc = AirbyteTracedException(
internal_message="internal message", message="user-friendly message", exception=RuntimeError("oh no")
)

expected_message = AirbyteMessage(
type="TRACE",
trace=AirbyteTraceMessage(
type="ERROR",
emitted_at=0.0,
error=AirbyteErrorTraceMessage(
failure_type="system_error",
message="user-friendly message",
internal_message="internal message",
stack_trace="RuntimeError: oh no\n",
),
),
)

traced_exc.emit_message()

stdout = capsys.readouterr().out
printed_message = AirbyteMessage.parse_obj(json.loads(stdout))
printed_message.trace.emitted_at = 0.0

assert printed_message == expected_message

0 comments on commit b00fc78

Please sign in to comment.