Skip to content

Commit

Permalink
Forward incoming error when decoding fails (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-villar authored Aug 22, 2024
1 parent 603bd86 commit 8d39738
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
32 changes: 18 additions & 14 deletions qiskit_transpiler_service/wrappers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,22 @@ 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}"

try:
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}"
except Exception:
# If something fails decoding the error
# just show the incoming error
msg = f"Internal error: {str(exc)}"
return msg
11 changes: 4 additions & 7 deletions tests/ai/test_clifford_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,16 @@ def test_clifford_wrong_token(random_circuit_transpiled, backend, caplog):


@pytest.mark.disable_monkeypatch
def test_clifford_wrong_url(random_circuit_transpiled, backend):
def test_clifford_wrong_url(random_circuit_transpiled, backend, caplog):
ai_optimize_cliff = PassManager(
[
CollectCliffords(),
AICliffordSynthesis(backend_name=backend, base_url="https://ibm.com/"),
]
)
try:
ai_optimized_circuit = ai_optimize_cliff.run(random_circuit_transpiled)
pytest.fail("Error expected")
except Exception as e:
assert "Expecting value: line 1 column 1 (char 0)" in str(e)
assert type(e).__name__ == "JSONDecodeError"
ai_optimized_circuit = ai_optimize_cliff.run(random_circuit_transpiled)
assert "Internal error: 404 Client Error:" in caplog.text
assert "Keeping the original circuit" in caplog.text


@pytest.mark.disable_monkeypatch
Expand Down
4 changes: 2 additions & 2 deletions tests/ai/test_routing_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def test_routing_wrong_url(qv_circ, backend):
ai_optimized_circuit = ai_optimize_lf.run(qv_circ)
pytest.fail("Error expected")
except Exception as e:
assert "Expecting value: line 1 column 1 (char 0)" in str(e)
assert type(e).__name__ == "JSONDecodeError"
assert "Internal error: 404 Client Error: Not Found for url" in str(e)
assert type(e).__name__ == "TranspilerError"


@pytest.mark.disable_monkeypatch
Expand Down
11 changes: 9 additions & 2 deletions tests/test_transpiler_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def test_rand_circ_cmap_routing(
):
random_circ = random_circuit(5, depth=3, seed=42).decompose(reps=3)

coupling_map.extend([item[::-1] for item in coupling_map])
cloud_transpiler_service = TranspilerService(
coupling_map=coupling_map,
ai=ai,
Expand Down Expand Up @@ -175,6 +176,9 @@ def test_transpile_non_valid_backend():
)


@pytest.mark.skip(
"Service accepts now 1e6 gates. Takes too much time to create that circuit."
)
def test_transpile_exceed_circuit_size():
circuit = EfficientSU2(120, entanglement="full", reps=5).decompose()
transpiler_service = TranspilerService(
Expand Down Expand Up @@ -239,8 +243,11 @@ def test_transpile_wrong_url():
transpiler_service.run(circuit)
pytest.fail("Error expected")
except Exception as e:
assert "Expecting value: line 1 column 1 (char 0)" in str(e)
assert type(e).__name__ == "JSONDecodeError"
assert (
"Internal error: 404 Client Error: Not Found for url: https://www.ibm.com/transpile"
in str(e)
)
assert type(e).__name__ == "TranspilerError"


@pytest.mark.disable_monkeypatch
Expand Down

0 comments on commit 8d39738

Please sign in to comment.