Skip to content

Commit

Permalink
Get message for exception in most cases, except when status 422 received
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Villar committed Aug 12, 2024
1 parent 603bd86 commit 1413b92
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
30 changes: 16 additions & 14 deletions qiskit_transpiler_service/wrappers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,20 @@ def _raise_transpiler_error_and_log(msg: str):


def _get_error_msg_from_response(exc: requests.exceptions.HTTPError):
resp = exc.response.json()
detail = resp.get("detail")
# Default message
msg = "Internal error."

if isinstance(detail, str):
msg = detail
elif isinstance(detail, list):
detail_input = detail[0]["input"]
detail_msg = detail[0]["msg"]

if detail_input and detail_msg:
msg = f"Wrong input '{detail_input}'. {detail_msg}"

if exc.response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY.value:
resp = exc.response.json()
detail = resp.get("detail")
# Default message
msg = "Internal error."

if isinstance(detail, str):
msg = detail
elif isinstance(detail, list):
detail_input = detail[0]["input"]
detail_msg = detail[0]["msg"]

if detail_input and detail_msg:
msg = f"Wrong input '{detail_input}'. {detail_msg}"
else:
msg = f"Internal error: {str(exc)}"
return msg
20 changes: 20 additions & 0 deletions tests/test_transpiler_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ def test_transpile_non_valid_backend():
)


def test_wrong_url():
random_circ = random_circuit(5, depth=2, seed=42)
transpiler_service = TranspilerService(
backend_name="ibm_kyoto",
ai="false",
optimization_level=3,
)

transpiler_service.transpiler_service.url = "https://cloud-transpiler-experimental.quantum.ibm.com/INVALID"

try:
transpiler_service.run(random_circ)
pytest.fail("Error expected")
except Exception as e:
assert (
"Internal error: 404 Client Error: Not Found for url:" in
str(e)
)


def test_transpile_exceed_circuit_size():
circuit = EfficientSU2(120, entanglement="full", reps=5).decompose()
transpiler_service = TranspilerService(
Expand Down

0 comments on commit 1413b92

Please sign in to comment.