Skip to content
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

implement error message in job #1528

Merged
merged 9 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 10 additions & 0 deletions client/qiskit_serverless/core/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
GATEWAY_PROVIDER_VERSION_DEFAULT,
)

from qiskit_serverless.exception import QiskitServerlessException
from qiskit_serverless.serializers.program_serializers import (
QiskitObjectsEncoder,
QiskitObjectsDecoder,
Expand Down Expand Up @@ -125,6 +126,10 @@ def filtered_logs(self, **kwargs) -> str:
"""
return self._client.filtered_logs(job_id=self.job_id, **kwargs)

def error_message(self):
"""Returns the execution error message."""
return self._client.result(self.job_id) if self.status() == "ERROR" else ""

def result(self, wait=True, cadence=5, verbose=False, maxwait=0):
"""Return results of the job.
Args:
Expand All @@ -145,6 +150,11 @@ def result(self, wait=True, cadence=5, verbose=False, maxwait=0):
if verbose:
logging.info(count)

if self.status() == "ERROR":
raise QiskitServerlessException(
korgan00 marked this conversation as resolved.
Show resolved Hide resolved
"Job finished with an error. Use error_message() to get additional information."
)

# Retrieve the results. If they're string format, try to decode to a dictionary.
results = self._client.result(self.job_id)
if isinstance(results, str):
Expand Down
6 changes: 5 additions & 1 deletion tests/basic/02_arguments_and_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
job = my_pattern_function.run(circuit=circuit)
print(job)

print(job.result())
try:
print(job.result())
except:
print(job.error_message())

print(job.status())
print(job.logs())
6 changes: 5 additions & 1 deletion tests/basic/04_distributed_workloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
job = my_pattern_function.run(circuits=circuits)
print(job)

print(job.result())
try:
print(job.result())
except:
print(job.error_message())

print(job.status())
print(job.logs())
18 changes: 15 additions & 3 deletions tests/experimental/running_programs_using_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ def hello_qiskit():

job = hello_qiskit()
print(job)
print(job.result())
try:
print(job.result())
except:
print(job.error_message())

print(job.status())
print(job.logs())

Expand All @@ -57,7 +61,11 @@ def function_with_distributed_tasks(circuits):

job = function_with_distributed_tasks(circuits=circuits)
print(job)
print(job.result())
try:
print(job.result())
except:
print(job.error_message())

print(job.status())
print(job.logs())

Expand All @@ -70,6 +78,10 @@ def my_function_with_modules():

job = my_function_with_modules()
print(job)
print(job.result())
try:
print(job.result())
except:
print(job.error_message())

print(job.status())
print(job.logs())