-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unify exception handling of requests library error and website …
…execution error. close #142
- Loading branch information
1 parent
ab9f4ac
commit c60f4c9
Showing
9 changed files
with
377 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
from .circuit_error import * | ||
from .quafu_error import * | ||
from .user_error import * | ||
from .utils import validate_server_resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from ..exceptions import QuafuError | ||
from .quafu_error import QuafuError | ||
|
||
|
||
class UserError(QuafuError): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# (C) Copyright 2023 Beijing Academy of Quantum Information Sciences | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from .circuit_error import IndexOutOfRangeError, InvalidParaError, UnsupportedYet | ||
from .quafu_error import CircuitError, CompileError, QuafuError, ServerError | ||
from .user_error import APITokenNotFound, BackendNotAvailable, UserError | ||
|
||
|
||
def validate_server_resp(res): | ||
"""Check results returned by backend service""" | ||
|
||
status_code = res["status"] if "status" in res else res["code"] | ||
err_msg = ( | ||
res["message"] | ||
if "message" in res | ||
else res["msg"] | ||
if "msg" in res | ||
else "No error message" | ||
) | ||
|
||
if status_code in [201, 205, 400]: | ||
raise UserError(err_msg) | ||
if status_code == 5001: | ||
raise CircuitError(err_msg) | ||
if status_code == 5003: | ||
raise ServerError(err_msg) | ||
if status_code == 5004: | ||
raise CompileError(err_msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# (C) Copyright 2023 Beijing Academy of Quantum Information Sciences | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import requests | ||
from requests.exceptions import RequestException | ||
|
||
from ..exceptions import ServerError | ||
|
||
|
||
class ClientWrapper: | ||
@staticmethod | ||
def post(*args, **kwargs): | ||
try: | ||
response = requests.post(*args, **kwargs) | ||
response.raise_for_status() | ||
except RequestException as err: | ||
raise ServerError( | ||
f"Failed to communicate with quafu website, please retry later or submit an issue, err: {err}" | ||
) from err | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
{ | ||
"data": [ | ||
{ | ||
"QV": 0, | ||
"clops": 0.0, | ||
"priority_qubits": null, | ||
"qubits": 10, | ||
"status": "Offline", | ||
"system_id": 0, | ||
"system_name": "ScQ-P10", | ||
"valid_gates": [ | ||
"cx", | ||
"cz", | ||
"rx", | ||
"ry", | ||
"rz", | ||
"x", | ||
"y", | ||
"z", | ||
"h", | ||
"sx", | ||
"sy", | ||
"id", | ||
"delay", | ||
"barrier", | ||
"cy", | ||
"cnot", | ||
"swap" | ||
] | ||
}, | ||
{ | ||
"QV": 0, | ||
"clops": 0.0, | ||
"priority_qubits": null, | ||
"qubits": 8, | ||
"status": "Maintenance", | ||
"system_id": 1, | ||
"system_name": "ScQ-P18", | ||
"valid_gates": [ | ||
"cx", | ||
"cz", | ||
"rx", | ||
"ry", | ||
"rz", | ||
"x", | ||
"y", | ||
"z", | ||
"h", | ||
"sx", | ||
"sy", | ||
"id", | ||
"delay", | ||
"barrier", | ||
"cy", | ||
"cnot", | ||
"swap" | ||
] | ||
}, | ||
{ | ||
"QV": 0, | ||
"clops": 0.0, | ||
"priority_qubits": "[108, 109, 119, 120, 121, 110, 111, 122, 123]", | ||
"qubits": 136, | ||
"status": "Online", | ||
"system_id": 2, | ||
"system_name": "ScQ-P136", | ||
"valid_gates": [ | ||
"cx", | ||
"cz", | ||
"rx", | ||
"ry", | ||
"rz", | ||
"x", | ||
"y", | ||
"z", | ||
"h", | ||
"delay", | ||
"barrier" | ||
] | ||
}, | ||
{ | ||
"QV": 0, | ||
"clops": 0.0, | ||
"priority_qubits": null, | ||
"qubits": 102, | ||
"status": "Maintenance", | ||
"system_id": 3, | ||
"system_name": "ScQ-P102", | ||
"valid_gates": [ | ||
"cx", | ||
"cz", | ||
"rx", | ||
"ry", | ||
"rz", | ||
"x", | ||
"y", | ||
"z", | ||
"h", | ||
"sx", | ||
"sy", | ||
"id", | ||
"delay", | ||
"barrier", | ||
"cy", | ||
"cnot", | ||
"swap" | ||
] | ||
}, | ||
{ | ||
"QV": 0, | ||
"clops": 0.0, | ||
"priority_qubits": null, | ||
"qubits": 10, | ||
"status": "Maintenance", | ||
"system_id": 4, | ||
"system_name": "ScQ-P10C", | ||
"valid_gates": [ | ||
"cx", | ||
"cz", | ||
"rx", | ||
"ry", | ||
"rz", | ||
"x", | ||
"y", | ||
"z", | ||
"h", | ||
"delay", | ||
"barrier" | ||
] | ||
}, | ||
{ | ||
"QV": 1, | ||
"clops": 1.0, | ||
"priority_qubits": null, | ||
"qubits": 2, | ||
"status": "Offline", | ||
"system_id": 5, | ||
"system_name": "ScQ-XXX", | ||
"valid_gates": [ | ||
"cx", | ||
"cz", | ||
"rx", | ||
"ry", | ||
"rz", | ||
"x", | ||
"y", | ||
"z", | ||
"h", | ||
"delay", | ||
"barrier" | ||
] | ||
}, | ||
{ | ||
"QV": 0, | ||
"clops": 0.0, | ||
"priority_qubits": "[1]", | ||
"qubits": 156, | ||
"status": "None Status", | ||
"system_id": 6, | ||
"system_name": "ScQ-P156", | ||
"valid_gates": [ | ||
"cx" | ||
] | ||
} | ||
], | ||
"message": "success", | ||
"ok": true, | ||
"status": 200 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"measure": "{108: 0, 109: 1}", | ||
"openqasm": "OPENQASM 2.0;\ninclude \"qelib1.inc\";\nqreg q[136];\ncreg c[2];\nh q[108];\ncx q[108],q[109];\nbarrier q[108],q[109];\nmeasure q[108] -> c[0];\nmeasure q[109] -> c[1];\n", | ||
"raw": "{\"11\": 851, \"00\": 998, \"10\": 90, \"01\": 61}", | ||
"res": "{\"11\": 851, \"00\": 998, \"10\": 90, \"01\": 61}", | ||
"status": 2, | ||
"task_id": "42FBBE201BA554DB", | ||
"task_name": "" | ||
} |
Oops, something went wrong.