api - updates errors and separates error from message in api errors responses

This commit is contained in:
Sergi Delgado Segura
2020-04-27 18:48:42 +02:00
parent 8918f15b62
commit 75ca281cc4
3 changed files with 33 additions and 26 deletions

View File

@@ -118,7 +118,7 @@ class API:
except InvalidParameter as e:
logger.info("Received invalid register request", from_addr="{}".format(remote_addr))
return jsonify({"error": str(e)}), HTTP_BAD_REQUEST
return jsonify({"error": str(e), "error_code": errors.INVALID_REQUEST_FORMAT}), HTTP_BAD_REQUEST
user_id = request_data.get("public_key")
@@ -134,13 +134,14 @@ class API:
except InvalidParameter as e:
rcode = HTTP_BAD_REQUEST
error = "Error {}: {}".format(errors.REGISTRATION_MISSING_FIELD, str(e))
response = {"error": error}
response = {"error": str(e), "error_code": errors.REGISTRATION_MISSING_FIELD}
else:
rcode = HTTP_BAD_REQUEST
error = "Error {}: public_key not found in register message".format(errors.REGISTRATION_WRONG_FIELD_FORMAT)
response = {"error": error}
response = {
"error": "public_key not found in register message",
"error_code": errors.REGISTRATION_WRONG_FIELD_FORMAT,
}
logger.info("Sending response and disconnecting", from_addr="{}".format(remote_addr), response=response)
@@ -169,7 +170,7 @@ class API:
request_data = get_request_data_json(request)
except InvalidParameter as e:
return jsonify({"error": str(e)}), HTTP_BAD_REQUEST
return jsonify({"error": str(e), "error_code": errors.INVALID_REQUEST_FORMAT}), HTTP_BAD_REQUEST
try:
appointment = self.inspector.inspect(request_data.get("appointment"))
@@ -178,16 +179,14 @@ class API:
except InspectionFailed as e:
rcode = HTTP_BAD_REQUEST
error = "appointment rejected. Error {}: {}".format(e.erno, e.reason)
response = {"error": error}
response = {"error": "appointment rejected. {}".format(e.reason), "error_code": e.erno}
except (AuthenticationFailure, NotEnoughSlots):
rcode = HTTP_BAD_REQUEST
error = "appointment rejected. Error {}: {}".format(
errors.APPOINTMENT_INVALID_SIGNATURE_OR_INSUFFICIENT_SLOTS,
"Invalid signature or user does not have enough slots available",
)
response = {"error": error}
response = {
"error": "appointment rejected. Invalid signature or user does not have enough slots available",
"error_code": errors.APPOINTMENT_INVALID_SIGNATURE_OR_INSUFFICIENT_SLOTS,
}
except AppointmentLimitReached:
rcode = HTTP_SERVICE_UNAVAILABLE
@@ -224,7 +223,7 @@ class API:
except InvalidParameter as e:
logger.info("Received invalid get_appointment request", from_addr="{}".format(remote_addr))
return jsonify({"error": str(e)}), HTTP_BAD_REQUEST
return jsonify({"error": str(e), "error_code": errors.INVALID_REQUEST_FORMAT}), HTTP_BAD_REQUEST
locator = request_data.get("locator")

View File

@@ -1,4 +1,4 @@
# Appointment errors [-1, -64]
# Appointment errors [-1, -32]
APPOINTMENT_EMPTY_FIELD = -1
APPOINTMENT_WRONG_FIELD_TYPE = -2
APPOINTMENT_WRONG_FIELD_SIZE = -3
@@ -8,12 +8,14 @@ APPOINTMENT_FIELD_TOO_BIG = -6
APPOINTMENT_WRONG_FIELD = -7
APPOINTMENT_INVALID_SIGNATURE_OR_INSUFFICIENT_SLOTS = -8
# Registration errors [-65, -128]
REGISTRATION_MISSING_FIELD = -65
REGISTRATION_WRONG_FIELD_FORMAT = -66
# Registration errors [-33, -64]
REGISTRATION_MISSING_FIELD = -33
REGISTRATION_WRONG_FIELD_FORMAT = -34
# Custom RPC errors
RPC_TX_REORGED_AFTER_BROADCAST = -98
# General errors [-65, -96]
INVALID_REQUEST_FORMAT = -65
# Custom RPC errors [255+]
RPC_TX_REORGED_AFTER_BROADCAST = -256
# UNHANDLED
UNKNOWN_JSON_RPC_EXCEPTION = -99
UNKNOWN_JSON_RPC_EXCEPTION = -257

View File

@@ -139,11 +139,13 @@ def test_register_wrong_client_pk(client):
def test_register_no_json(client):
r = client.post(register_endpoint, data="random_message")
assert r.status_code == HTTP_BAD_REQUEST
assert errors.INVALID_REQUEST_FORMAT == r.json.get("error_code")
def test_register_json_no_inner_dict(client):
r = client.post(register_endpoint, json="random_message")
assert r.status_code == HTTP_BAD_REQUEST
assert errors.INVALID_REQUEST_FORMAT == r.json.get("error_code")
def test_add_appointment(api, client, appointment):
@@ -165,16 +167,18 @@ def test_add_appointment_no_json(api, client, appointment):
r = client.post(add_appointment_endpoint, data="random_message")
assert r.status_code == HTTP_BAD_REQUEST
assert "Request is not json encoded" in r.json.get("error")
assert errors.INVALID_REQUEST_FORMAT == r.json.get("error_code")
def test_add_appointment_json_no_inner_dict(api, client, appointment):
# Simulate the user registration (end time does not matter here)
api.watcher.gatekeeper.registered_users[user_id] = UserInfo(available_slots=1, subscription_expiry=0)
# JSON data with no inner dict (invalid data foramat)
# JSON data with no inner dict (invalid data format)
r = client.post(add_appointment_endpoint, json="random_message")
assert r.status_code == HTTP_BAD_REQUEST
assert "Invalid request content" in r.json.get("error")
assert errors.INVALID_REQUEST_FORMAT == r.json.get("error_code")
def test_add_appointment_wrong(api, client, appointment):
@@ -186,7 +190,7 @@ def test_add_appointment_wrong(api, client, appointment):
appointment_signature = Cryptographer.sign(appointment.serialize(), user_sk)
r = add_appointment(client, {"appointment": appointment.to_dict(), "signature": appointment_signature}, user_id)
assert r.status_code == HTTP_BAD_REQUEST
assert "Error {}:".format(errors.APPOINTMENT_FIELD_TOO_SMALL) in r.json.get("error")
assert errors.APPOINTMENT_FIELD_TOO_SMALL == r.json.get("error_code")
def test_add_appointment_not_registered(api, client, appointment):
@@ -199,7 +203,7 @@ def test_add_appointment_not_registered(api, client, appointment):
client, {"appointment": appointment.to_dict(), "signature": appointment_signature}, tmp_compressed_pk
)
assert r.status_code == HTTP_BAD_REQUEST
assert "Error {}:".format(errors.APPOINTMENT_INVALID_SIGNATURE_OR_INSUFFICIENT_SLOTS) in r.json.get("error")
assert errors.APPOINTMENT_INVALID_SIGNATURE_OR_INSUFFICIENT_SLOTS == r.json.get("error_code")
def test_add_appointment_registered_no_free_slots(api, client, appointment):
@@ -210,7 +214,7 @@ def test_add_appointment_registered_no_free_slots(api, client, appointment):
appointment_signature = Cryptographer.sign(appointment.serialize(), user_sk)
r = add_appointment(client, {"appointment": appointment.to_dict(), "signature": appointment_signature}, user_id)
assert r.status_code == HTTP_BAD_REQUEST
assert "Error {}:".format(errors.APPOINTMENT_INVALID_SIGNATURE_OR_INSUFFICIENT_SLOTS) in r.json.get("error")
assert errors.APPOINTMENT_INVALID_SIGNATURE_OR_INSUFFICIENT_SLOTS == r.json.get("error_code")
def test_add_appointment_registered_not_enough_free_slots(api, client, appointment):
@@ -225,7 +229,7 @@ def test_add_appointment_registered_not_enough_free_slots(api, client, appointme
r = add_appointment(client, {"appointment": appointment.to_dict(), "signature": appointment_signature}, user_id)
assert r.status_code == HTTP_BAD_REQUEST
assert "Error {}:".format(errors.APPOINTMENT_INVALID_SIGNATURE_OR_INSUFFICIENT_SLOTS) in r.json.get("error")
assert errors.APPOINTMENT_INVALID_SIGNATURE_OR_INSUFFICIENT_SLOTS == r.json.get("error_code")
def test_add_appointment_multiple_times_same_user(api, client, appointment, n=MULTIPLE_APPOINTMENTS):
@@ -342,12 +346,14 @@ def test_get_appointment_no_json(api, client, appointment):
r = client.post(add_appointment_endpoint, data="random_message")
assert r.status_code == HTTP_BAD_REQUEST
assert "Request is not json encoded" in r.json.get("error")
assert errors.INVALID_REQUEST_FORMAT == r.json.get("error_code")
def test_get_appointment_json_no_inner_dict(api, client, appointment):
r = client.post(add_appointment_endpoint, json="random_message")
assert r.status_code == HTTP_BAD_REQUEST
assert "Invalid request content" in r.json.get("error")
assert errors.INVALID_REQUEST_FORMAT == r.json.get("error_code")
def test_get_random_appointment_registered_user(client, user_sk=user_sk):