From 8d3973874f39974221a2183d3bb13364d315f18c Mon Sep 17 00:00:00 2001 From: victor-villar Date: Thu, 22 Aug 2024 11:03:32 -0400 Subject: [PATCH] Forward incoming error when decoding fails (#36) --- qiskit_transpiler_service/wrappers/base.py | 32 ++++++++++++---------- tests/ai/test_clifford_ai.py | 11 +++----- tests/ai/test_routing_ai.py | 4 +-- tests/test_transpiler_service.py | 11 ++++++-- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/qiskit_transpiler_service/wrappers/base.py b/qiskit_transpiler_service/wrappers/base.py index 761badd..3225702 100644 --- a/qiskit_transpiler_service/wrappers/base.py +++ b/qiskit_transpiler_service/wrappers/base.py @@ -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 diff --git a/tests/ai/test_clifford_ai.py b/tests/ai/test_clifford_ai.py index c38c0e9..a43f2d8 100644 --- a/tests/ai/test_clifford_ai.py +++ b/tests/ai/test_clifford_ai.py @@ -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 diff --git a/tests/ai/test_routing_ai.py b/tests/ai/test_routing_ai.py index 78e357a..6f627bb 100644 --- a/tests/ai/test_routing_ai.py +++ b/tests/ai/test_routing_ai.py @@ -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 diff --git a/tests/test_transpiler_service.py b/tests/test_transpiler_service.py index 9d39fb6..b282541 100644 --- a/tests/test_transpiler_service.py +++ b/tests/test_transpiler_service.py @@ -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, @@ -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( @@ -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