Improved API CRUD POST and PATCH handeling of data validation errors.

This commit is contained in:
Scott Idem
2023-12-19 12:30:51 -05:00
parent be6d361bf7
commit 55d953b445

View File

@@ -569,9 +569,18 @@ async def patch_obj(
if run_safety_check:
log.info('Running safety check by default')
base_name = obj_type_li[obj_name]['base_name']
obj_model = base_name(**crud.data_list) # .dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
try:
obj_model = base_name(**crud.data_list)
except Exception as e:
log.error('An unknown exception happened. Returning False.')
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
log.warning(json.dumps(crud.data_list, indent=4))
return mk_resp(data=False, status_code=400, response=commons.response, status_message='There was likely a validation error. Returned False.')
else:
log.info('Successfully updated the object model.')
pass
log.debug(obj_model)
# obj_dict = obj_model.dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset, exclude=exclude)
obj_dict = obj_model.dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset, include=field_list)
log.debug(obj_dict)
crud_data = obj_dict
@@ -713,16 +722,17 @@ async def post_obj(
log.info('Running safety check by default')
base_name = obj_type_li[obj_name]['base_name']
try:
obj_model = base_name(**crud.data_list) # .dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
obj_model = base_name(**crud.data_list)
except Exception as e:
log.error('An unknown exception happened. Returning False.')
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
log.warning(json.dumps(crud.data_list, indent=4))
return mk_resp(data=False, status_code=400, response=commons.response, status_message='There was likely a validation error. Returned False.')
else:
log.info('Successfully created the object model.')
pass
log.debug(obj_model)
# obj_dict = obj_model.dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset, exclude=exclude)
obj_dict = obj_model.dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset, include=field_list)
log.debug(obj_dict)
crud_data = obj_dict