Skip to content

Commit

Permalink
implement error message in job (#1528)
Browse files Browse the repository at this point in the history
* implement error message in job

* print traces

* print traces

* fix tests

* fix tests

* fix tests

* changed exception content

* change client to job_service
  • Loading branch information
korgan00 authored Oct 30, 2024
1 parent 6f7a5f1 commit f96e432
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
9 changes: 9 additions & 0 deletions client/qiskit_serverless/core/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
GATEWAY_PROVIDER_VERSION_DEFAULT,
)

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

def error_message(self):
"""Returns the execution error message."""
return self._job_service.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 @@ -179,6 +184,10 @@ def result(self, wait=True, cadence=5, verbose=False, maxwait=0):

# Retrieve the results. If they're string format, try to decode to a dictionary.
results = self._job_service.result(self.job_id)

if self.status() == "ERROR":
raise QiskitServerlessException(results)

if isinstance(results, str):
try:
results = json.loads(results, cls=QiskitObjectsDecoder)
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())

0 comments on commit f96e432

Please sign in to comment.