Skip to content

Commit

Permalink
Use error code 1305 instead of hard coding error msg (#1760)
Browse files Browse the repository at this point in the history
* Use error code 1305

* fix lint

* Update unit test

* handle both channels
  • Loading branch information
kt474 authored Jul 8, 2024
1 parent 5436f0f commit 475b96f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
14 changes: 7 additions & 7 deletions qiskit_ibm_runtime/base_runtime_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def __init__(
self._creation_date = creation_date
self._program_id = program_id
self._reason: Optional[str] = None
self._reason_code: Optional[int] = None
self._error_message: Optional[str] = None
self._image = image
self._final_interim_results = False
Expand Down Expand Up @@ -239,15 +240,14 @@ def _set_status(self, job_response: Dict) -> None:
"""
try:
reason = job_response["state"].get("reason")
reason_code = job_response["state"].get("reason_code")
reason_code = job_response["state"].get("reasonCode") or job_response["state"].get(
"reason_code"
)
if reason:
# TODO remove this in https://github.com/Qiskit/qiskit-ibm-runtime/issues/989
if reason.upper() == "RAN TOO LONG":
self._reason = reason.upper()
else:
self._reason = reason
self._reason = reason
if reason_code:
self._reason = f"Error code {reason_code}; {self._reason}"
self._reason_code = reason_code
self._status = self._status_from_job_response(job_response)
except KeyError:
raise IBMError(f"Unknown status: {job_response['state']['status']}")
Expand Down Expand Up @@ -286,7 +286,7 @@ def _error_msg_from_job_response(self, response: Dict) -> str:
if index != -1:
job_result_raw = job_result_raw[index:]

if status == "CANCELLED" and self._reason == "RAN TOO LONG":
if status == "CANCELLED" and self._reason_code == 1305:
error_msg = API_TO_JOB_ERROR_MESSAGE["CANCELLED - RAN TOO LONG"]
return error_msg.format(self.job_id(), job_result_raw)
else:
Expand Down
6 changes: 3 additions & 3 deletions qiskit_ibm_runtime/runtime_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ def result( # pylint: disable=arguments-differ
self.wait_for_final_state(timeout=timeout)
if self._status == self.ERROR:
error_message = self._reason if self._reason else self._error_message
if self._reason == "RAN TOO LONG":
raise RuntimeJobMaxTimeoutError(error_message)
if self._reason_code == 1305:
raise RuntimeJobMaxTimeoutError(self._error_message)
raise RuntimeJobFailureError(f"Unable to retrieve job result. {error_message}")
if self._status is JobStatus.CANCELLED:
raise RuntimeInvalidStateError(
Expand Down Expand Up @@ -216,7 +216,7 @@ def _status_from_job_response(self, response: Dict) -> str:
Job status.
"""
mapped_job_status = API_TO_JOB_STATUS[response["state"]["status"].upper()]
if mapped_job_status == JobStatus.CANCELLED and self._reason == "RAN TOO LONG":
if mapped_job_status == JobStatus.CANCELLED and self._reason_code == 1305:
mapped_job_status = self.ERROR
return mapped_job_status

Expand Down
4 changes: 2 additions & 2 deletions qiskit_ibm_runtime/runtime_job_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def result( # pylint: disable=arguments-differ
self.wait_for_final_state(timeout=timeout)
if self._status == "ERROR":
error_message = self._reason if self._reason else self._error_message
if self._reason == "RAN TOO LONG":
if self._reason_code == 1305:
raise RuntimeJobMaxTimeoutError(error_message)
raise RuntimeJobFailureError(f"Unable to retrieve job result. {error_message}")
if self._status == "CANCELLED":
Expand Down Expand Up @@ -187,7 +187,7 @@ def _status_from_job_response(self, response: Dict) -> Union[JobStatus, str]:
api_status = response["state"]["status"].upper()
if api_status in API_TO_JOB_STATUS:
mapped_job_status = API_TO_JOB_STATUS[api_status]
if mapped_job_status == "CANCELLED" and self._reason == "RAN TOO LONG":
if mapped_job_status == "CANCELLED" and self._reason_code == 1305:
mapped_job_status = "ERROR"
return mapped_job_status
return api_status
Expand Down
2 changes: 1 addition & 1 deletion test/ibm_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def _run_program(
isa_qc = pm.run(bell())
job = sampler.run([isa_qc])
else:
job = service.run(
job = service._run(
program_id=pid,
inputs=inputs,
options=options,
Expand Down
2 changes: 1 addition & 1 deletion test/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def run_program(
service._api_client.set_job_classes(job_classes)
if not program_id:
program_id = "sampler"
job = service.run(
job = service._run(
program_id=program_id,
options=options,
inputs=inputs,
Expand Down
8 changes: 7 additions & 1 deletion test/unit/mock/fake_runtime_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(
self._job_id = job_id
self._status = final_status or "QUEUED"
self._reason: Optional[str] = None
self._reason_code: Optional[int] = None
self._program_id = program_id
self._hub = hub
self._group = group
Expand Down Expand Up @@ -151,7 +152,11 @@ def to_dict(self):
"group": self._group,
"project": self._project,
"backend": self._backend_name,
"state": {"status": self._status, "reason": self._reason},
"state": {
"status": self._status,
"reason": self._reason,
"reasonCode": self._reason_code,
},
"params": self._params,
"program": {"id": self._program_id},
"image": self._image,
Expand Down Expand Up @@ -194,6 +199,7 @@ def _auto_progress(self):

if self._status == "CANCELLED":
self._reason = "RAN TOO LONG"
self._reason_code = 1305
self._result = "Kaboom!"


Expand Down

0 comments on commit 475b96f

Please sign in to comment.