properly serializing ValidationError exceptions.
This commit is contained in:
parent
1f854b6cde
commit
ff060edcfa
@ -7,6 +7,7 @@ from ..services.auth_guard import auth_guard, check_jwt
|
|||||||
from BreCal.validators.input_validation import validate_posted_shipcall_data, check_if_user_is_bsmd_type
|
from BreCal.validators.input_validation import validate_posted_shipcall_data, check_if_user_is_bsmd_type
|
||||||
from BreCal.validators.input_validation_shipcall import InputValidationShipcall
|
from BreCal.validators.input_validation_shipcall import InputValidationShipcall
|
||||||
from BreCal.database.sql_handler import execute_sql_query_standalone
|
from BreCal.database.sql_handler import execute_sql_query_standalone
|
||||||
|
from BreCal.validators.validation_error import create_validation_error_response, create_werkzeug_error_response
|
||||||
from . import verify_if_request_is_json
|
from . import verify_if_request_is_json
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
@ -58,7 +59,7 @@ def PostShipcalls():
|
|||||||
except ValidationError as ex:
|
except ValidationError as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
print(ex)
|
print(ex)
|
||||||
return json.dumps({"message":f"bad format. \nError Messages: {ex.messages}. \nValid Data: {ex.valid_data}"}), 400
|
return create_validation_error_response(ex=ex, status_code=400)
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
@ -88,12 +89,12 @@ def PutShipcalls():
|
|||||||
except ValidationError as ex:
|
except ValidationError as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
print(ex)
|
print(ex)
|
||||||
return json.dumps({"message":f"bad format. \nError Messages: {ex.messages}. \nValid Data: {ex.valid_data}"}), 400
|
return create_validation_error_response(ex=ex, status_code=400)
|
||||||
|
|
||||||
except werkzeug.exceptions.Forbidden as ex:
|
except werkzeug.exceptions.Forbidden as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
print(ex)
|
print(ex)
|
||||||
return json.dumps({"message":ex.description}), 403
|
return create_werkzeug_error_response(ex=ex, status_code=403)
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
|
|||||||
@ -6,6 +6,7 @@ from ..schemas import model
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from . import verify_if_request_is_json
|
from . import verify_if_request_is_json
|
||||||
|
from BreCal.validators.validation_error import create_validation_error_response
|
||||||
|
|
||||||
from BreCal.validators.input_validation import check_if_user_is_bsmd_type
|
from BreCal.validators.input_validation import check_if_user_is_bsmd_type
|
||||||
from BreCal.validators.input_validation_ship import InputValidationShip
|
from BreCal.validators.input_validation_ship import InputValidationShip
|
||||||
@ -45,6 +46,11 @@ def PostShip():
|
|||||||
# validate the request data & user permissions
|
# validate the request data & user permissions
|
||||||
InputValidationShip.evaluate_post_data(user_data, loadedModel, content)
|
InputValidationShip.evaluate_post_data(user_data, loadedModel, content)
|
||||||
|
|
||||||
|
except ValidationError as ex:
|
||||||
|
logging.error(ex)
|
||||||
|
print(ex)
|
||||||
|
return create_validation_error_response(ex=ex, status_code=400)
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
print(ex)
|
print(ex)
|
||||||
@ -69,6 +75,11 @@ def PutShip():
|
|||||||
# validate the request data & user permissions
|
# validate the request data & user permissions
|
||||||
InputValidationShip.evaluate_put_data(user_data, loadedModel, content)
|
InputValidationShip.evaluate_put_data(user_data, loadedModel, content)
|
||||||
|
|
||||||
|
except ValidationError as ex:
|
||||||
|
logging.error(ex)
|
||||||
|
print(ex)
|
||||||
|
return create_validation_error_response(ex=ex, status_code=400)
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
print(ex)
|
print(ex)
|
||||||
@ -97,6 +108,11 @@ def DeleteShip():
|
|||||||
# validate the request data & user permissions
|
# validate the request data & user permissions
|
||||||
InputValidationShip.evaluate_delete_data(user_data, ship_id)
|
InputValidationShip.evaluate_delete_data(user_data, ship_id)
|
||||||
|
|
||||||
|
except ValidationError as ex:
|
||||||
|
logging.error(ex)
|
||||||
|
print(ex)
|
||||||
|
return create_validation_error_response(ex=ex, status_code=400)
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
print(ex)
|
print(ex)
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import logging
|
|||||||
from marshmallow import ValidationError
|
from marshmallow import ValidationError
|
||||||
from BreCal.validators.input_validation_times import InputValidationTimes
|
from BreCal.validators.input_validation_times import InputValidationTimes
|
||||||
from . import verify_if_request_is_json
|
from . import verify_if_request_is_json
|
||||||
|
from BreCal.validators.validation_error import create_validation_error_response, create_werkzeug_error_response
|
||||||
|
|
||||||
bp = Blueprint('times', __name__)
|
bp = Blueprint('times', __name__)
|
||||||
|
|
||||||
@ -44,7 +45,7 @@ def PostTimes():
|
|||||||
except ValidationError as ex:
|
except ValidationError as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
print(ex)
|
print(ex)
|
||||||
return json.dumps(f"bad format. \nError Messages: {ex.messages}. \nValid Data: {ex.valid_data}"), 400
|
return create_validation_error_response(ex=ex, status_code=400)
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
@ -73,7 +74,7 @@ def PutTimes():
|
|||||||
except ValidationError as ex:
|
except ValidationError as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
print(ex)
|
print(ex)
|
||||||
return json.dumps(f"bad format. \nError Messages: {ex.messages}. \nValid Data: {ex.valid_data}"), 400
|
return create_validation_error_response(ex=ex, status_code=400)
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
@ -87,17 +88,28 @@ def PutTimes():
|
|||||||
@auth_guard() # no restriction by role
|
@auth_guard() # no restriction by role
|
||||||
def DeleteTimes():
|
def DeleteTimes():
|
||||||
|
|
||||||
if 'id' in request.args:
|
try:
|
||||||
options = {}
|
if 'id' in request.args:
|
||||||
options["id"] = request.args.get("id")
|
options = {}
|
||||||
|
options["id"] = request.args.get("id")
|
||||||
|
|
||||||
# read the user data from the JWT token (set when login is performed)
|
# read the user data from the JWT token (set when login is performed)
|
||||||
user_data = check_jwt()
|
user_data = check_jwt()
|
||||||
|
|
||||||
# validate the request
|
# validate the request
|
||||||
InputValidationTimes.evaluate_delete_data(user_data, times_id = request.args.get("id"))
|
InputValidationTimes.evaluate_delete_data(user_data, times_id = request.args.get("id"))
|
||||||
|
|
||||||
return impl.times.DeleteTimes(options)
|
return impl.times.DeleteTimes(options)
|
||||||
else:
|
else:
|
||||||
logging.warning("Times delete missing id argument")
|
logging.warning("Times delete missing id argument")
|
||||||
return json.dumps("missing argument"), 400
|
return json.dumps("missing argument"), 400
|
||||||
|
|
||||||
|
except ValidationError as ex:
|
||||||
|
logging.error(ex)
|
||||||
|
print(ex)
|
||||||
|
return create_validation_error_response(ex=ex, status_code=400)
|
||||||
|
|
||||||
|
except Exception as ex:
|
||||||
|
logging.error(ex)
|
||||||
|
print(ex)
|
||||||
|
return json.dumps("bad format"), 400
|
||||||
|
|||||||
@ -102,6 +102,9 @@ class InputValidationShip():
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def put_content_may_not_contain_imo_number(content:dict):
|
def put_content_may_not_contain_imo_number(content:dict):
|
||||||
put_data_ship_imo = content.get("imo",None)
|
put_data_ship_imo = content.get("imo",None)
|
||||||
|
|
||||||
|
# #TODO: Aktuelle IMO abfragen und nach Änderung suchen, bevor eine Fehlermeldung erstellt wird
|
||||||
|
|
||||||
if put_data_ship_imo is not None:
|
if put_data_ship_imo is not None:
|
||||||
raise ValidationError(f"The IMO number field may not be changed since it serves the purpose of a primary (matching) key.")
|
raise ValidationError(f"The IMO number field may not be changed since it serves the purpose of a primary (matching) key.")
|
||||||
return
|
return
|
||||||
|
|||||||
34
src/server/BreCal/validators/validation_error.py
Normal file
34
src/server/BreCal/validators/validation_error.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import logging
|
||||||
|
import typing
|
||||||
|
import json
|
||||||
|
from marshmallow import ValidationError
|
||||||
|
import werkzeug
|
||||||
|
from werkzeug.exceptions import Forbidden
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def create_validation_error_response(ex:ValidationError, status_code:int=400)->typing.Tuple[str,int]:
|
||||||
|
# generate an overview the errors
|
||||||
|
#example:
|
||||||
|
# {'lock_time': ['The provided value must be in the future. Current Time: 2024-09-02 08:23:32.600791, Value: 2024-09-01 08:20:41.853000']}
|
||||||
|
errors = ex.messages
|
||||||
|
|
||||||
|
# example:
|
||||||
|
# "Valid Data": {
|
||||||
|
# "id": 2894,
|
||||||
|
# "eta_berth": "datetime.datetime(2024, 9, 2, 11, 11, 43)",
|
||||||
|
# "eta_berth_fixed": false
|
||||||
|
# }
|
||||||
|
valid_data = ex.valid_data
|
||||||
|
|
||||||
|
|
||||||
|
json_response = {
|
||||||
|
"message":"ValidationError",
|
||||||
|
"errors":errors,
|
||||||
|
"valid_data":valid_data
|
||||||
|
}
|
||||||
|
return (json.dumps(json_response), status_code)
|
||||||
|
|
||||||
|
def create_werkzeug_error_response(ex:Forbidden, status_code:int=403)->typing.Tuple[str,int]:
|
||||||
|
return json.dumps({"message":ex.description}), status_code
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user