From f96e43287c199be092335ee3708f040fcf2bc808 Mon Sep 17 00:00:00 2001 From: Goyo Date: Wed, 30 Oct 2024 12:01:11 +0100 Subject: [PATCH] implement error message in job (#1528) * implement error message in job * print traces * print traces * fix tests * fix tests * fix tests * changed exception content * change client to job_service --- client/qiskit_serverless/core/job.py | 9 +++++++++ tests/basic/02_arguments_and_results.py | 6 +++++- tests/basic/04_distributed_workloads.py | 6 +++++- .../running_programs_using_decorators.py | 18 +++++++++++++++--- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/client/qiskit_serverless/core/job.py b/client/qiskit_serverless/core/job.py index 7b113e364..beec864fd 100644 --- a/client/qiskit_serverless/core/job.py +++ b/client/qiskit_serverless/core/job.py @@ -51,6 +51,7 @@ GATEWAY_PROVIDER_VERSION_DEFAULT, ) +from qiskit_serverless.exception import QiskitServerlessException from qiskit_serverless.serializers.program_serializers import ( QiskitObjectsEncoder, QiskitObjectsDecoder, @@ -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: @@ -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) diff --git a/tests/basic/02_arguments_and_results.py b/tests/basic/02_arguments_and_results.py index ee3572375..e9babe445 100644 --- a/tests/basic/02_arguments_and_results.py +++ b/tests/basic/02_arguments_and_results.py @@ -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()) diff --git a/tests/basic/04_distributed_workloads.py b/tests/basic/04_distributed_workloads.py index ca9d87248..ae3c49d64 100644 --- a/tests/basic/04_distributed_workloads.py +++ b/tests/basic/04_distributed_workloads.py @@ -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()) diff --git a/tests/experimental/running_programs_using_decorators.py b/tests/experimental/running_programs_using_decorators.py index 28cd4f981..19ac25d32 100644 --- a/tests/experimental/running_programs_using_decorators.py +++ b/tests/experimental/running_programs_using_decorators.py @@ -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()) @@ -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()) @@ -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())