Skip to content

Prettify error message on jobs #1662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion client/qiskit_serverless/core/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,16 @@ def filtered_logs(self, **kwargs) -> str:

def error_message(self):
"""Returns the execution error message."""
return self._job_service.result(self.job_id) if self.status() == "ERROR" else ""
error = (
self._job_service.result(self.job_id) if self.status() == "ERROR" else ""
)
try:
pretty_error = error.strip('"').encode().decode("unicode_escape")
except UnicodeDecodeError:
logging.warning("Error decoding error message, returning raw message.")
return error

return pretty_error

def result(self, wait=True, cadence=30, verbose=False, maxwait=0):
"""Return results of the job.
Expand Down
25 changes: 25 additions & 0 deletions client/tests/core/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ENV_ACCESS_TRIAL,
)
from qiskit_serverless.core.job import (
Job,
is_running_in_serverless,
save_result,
is_trial,
Expand Down Expand Up @@ -104,6 +105,30 @@ def test_filtered_logs(self):
"id", include="This is the l.+", exclude="the.+a.+l"
)

@patch("requests.get", Mock(return_value=ResponseMock()))
def test_error_message(self):
"""Tests job filtered log."""
client = ServerlessClient(host="host", token="token", version="version")
client.status = MagicMock(
return_value="ERROR",
)
client.result = MagicMock(
return_value=(
'"This is the line \\"1\\"\\n'
"This is the second line\\n"
'OK. This is the last line.\\n"'
),
)
job = Job(
job_id="job_id",
job_service=client,
)
assert (
'This is the line "1"\n'
"This is the second line\n"
"OK. This is the last line.\n"
) == job.error_message()


class TestRunningAsServerlessProgram:
"""Test ``is_running_in_serverless()``."""
Expand Down
Loading