Skip to content

Commit

Permalink
Merge pull request #4619 from bcgov/dev-marshal-AH-4539
Browse files Browse the repository at this point in the history
Ticket 4539. Fix/Adjustment to exception handling in request-management api
  • Loading branch information
Aman-Hundal authored Oct 23, 2023
2 parents 88e4885 + 91cea4b commit 3ada012
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 137 deletions.
19 changes: 10 additions & 9 deletions request-management-api/request_api/resources/foiadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

API = Namespace('FOIAdmin', description='Endpoints for FOI admin management')
TRACER = Tracer.get_instance()
CUSTOM_KEYERROR_MESSAGE = "Key error has occured: "

"""Custom exception messages
"""
Expand All @@ -50,9 +51,9 @@ def get():
try:
result = programareadivisionservice().getallprogramareadivisions()
return json.dumps(result), 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except BusinessException as exception:
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

@cors_preflight('POST,OPTIONS')
Expand All @@ -73,8 +74,8 @@ def post():
# if result.success == True:
# asyncio.ensure_future();
return {'status': result.success, 'message':result.message, 'id':result.identifier}, 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -97,8 +98,8 @@ def put(divisionid):
# if result.success == True:
# asyncio.ensure_future();
return {'status': result.success, 'message':result.message, 'id':result.identifier}, 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -118,8 +119,8 @@ def put(divisionid):
# if result.success == True:
# asyncio.ensure_future();
return {'status': result.success, 'message':result.message, 'id':result.identifier}, 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

21 changes: 11 additions & 10 deletions request-management-api/request_api/resources/foicfrfee.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"""Custom exception messages
"""
EXCEPTION_MESSAGE_BAD_REQUEST='Bad Request'
CUSTOM_KEYERROR_MESSAGE = "Key error has occured: "

@cors_preflight('POST,OPTIONS')
@API.route('/foicfrfee/foirequest/<requestid>/ministryrequest/<ministryrequestid>')
Expand All @@ -57,10 +58,10 @@ def post(requestid, ministryrequestid):
return {'status': result.success, 'message':result.message,'id':result.identifier} , 200
except ValidationError as verr:
logging.error(verr)
return {'status': False, 'message':verr.messages}, 400
except KeyError as err:
logging.error(err)
return {'status': False, 'message': EXCEPTION_MESSAGE_BAD_REQUEST}, 400
return {'status': False, 'message': str(verr)}, 400
except KeyError as error:
logging.error(CUSTOM_KEYERROR_MESSAGE + str(error))
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -87,10 +88,10 @@ def post(requestid, ministryrequestid):
return {'status': result.success, 'message':result.message,'id':result.identifier} , 200
except ValidationError as verr:
logging.error(verr)
return {'status': False, 'message':verr.messages}, 400
except KeyError as err:
logging.error(err)
return {'status': False, 'message': EXCEPTION_MESSAGE_BAD_REQUEST}, 400
return {'status': False, 'message': str(verr)}, 400
except KeyError as error:
logging.error(CUSTOM_KEYERROR_MESSAGE + str(error))
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -109,7 +110,7 @@ def get(requestid):
try:
result = {"current": cfrfeeservice().getcfrfee(requestid), "history": cfrfeeservice().getcfrfeehistory(requestid)}
return json.dumps(result), 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500
25 changes: 13 additions & 12 deletions request-management-api/request_api/resources/foicomment.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"""Custom exception messages
"""
EXCEPTION_MESSAGE_BAD_REQUEST='Bad Request'
CUSTOM_KEYERROR_MESSAGE = "Key error has occured: "

@cors_preflight('POST,OPTIONS')
@API.route('/foicomment/ministryrequest')
Expand All @@ -54,8 +55,8 @@ def post():
if result.success == True:
asyncio.ensure_future(eventservice().postcommentevent(result.identifier, "ministryrequest", AuthHelper.getuserid()))
return {'status': result.success, 'message':result.message,'id':result.identifier} , 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -77,8 +78,8 @@ def post():
if result.success == True:
asyncio.ensure_future(eventservice().postcommentevent(result.identifier, "rawrequest", AuthHelper.getuserid()))
return {'status': result.success, 'message':result.message,'id':result.identifier} , 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand Down Expand Up @@ -108,8 +109,8 @@ def get(requesttype, requestid):
return json.dumps(result), 200
else:
return {'status': 401, 'message':'Restricted Request'} , 401
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -136,8 +137,8 @@ def put(requesttype, commentid):
if result.success == True:
asyncio.ensure_future(eventservice().postcommentevent(result.identifier, requesttype, AuthHelper.getuserid(), True))
return {'status': result.success, 'message':result.message,'id':result.identifier} , 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand Down Expand Up @@ -166,8 +167,8 @@ def put(requesttype, commentid):
if result.success == True:
asyncio.ensure_future(eventservice().postcommentevent(commentid, requesttype, AuthHelper.getuserid(), existingtaggedusers=result.args[0]))
return {'status': result.success, 'message':result.message,'id':result.identifier} , 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -184,7 +185,7 @@ def get(requestid=None):
result = commentservice().createcommenttagginguserlist("rawrequest",requestid)
return json.dumps(result), 200
except ValueError:
return {'status': 500, 'message':"Invalid Request"}, 400
return {'status': 500, 'message':"Invalid Request"}, 500
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -200,6 +201,6 @@ def get(ministryrequestid=None):
result = commentservice().createcommenttagginguserlist("ministryrequest",ministryrequestid)
return json.dumps(result), 200
except ValueError:
return {'status': 500, 'message':"Invalid Request"}, 400
return {'status': 500, 'message':"Invalid Request"}, 500
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500
67 changes: 53 additions & 14 deletions request-management-api/request_api/resources/foidocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@


API = Namespace('FOIDocument', description='Endpoints for FOI Document management')
TRACER = Tracer.get_instance()
TRACER = Tracer.get_instance()
CUSTOM_KEYERROR_MESSAGE = "Key error has occured: "


@cors_preflight('GET,OPTIONS')
Expand All @@ -49,8 +50,8 @@ def get(requestid, requesttype):
try:
result = documentservice().getrequestdocumentsbyrole(requestid, requesttype, AuthHelper.isministrymember())
return json.dumps(result), 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -73,9 +74,9 @@ def post(requestid, requesttype):
result = documentservice().createrequestdocument(requestid, documentschema, AuthHelper.getuserid(), requesttype)
return {'status': result.success, 'message':result.message} , 200
except ValidationError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
return {'status': False, 'message': str(err)}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -97,12 +98,50 @@ def post(requestid, documentid, requesttype):
result = documentservice().createrequestdocumentversion(requestid, documentid, documentschema, AuthHelper.getuserid(), requesttype)
return {'status': result.success, 'message':result.message,'id':result.identifier} , 200
except ValidationError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
return {'status': False, 'message': str(err)}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

@cors_preflight('POST,OPTIONS')
@API.route('/foidocument/<requesttype>/<requestid>/documentid/<documentid>/reclassify')
class ReclassifyFOIDocument(Resource):
"""Resource for reclassifying uploaded attachments of FOI requests."""

@staticmethod
@TRACER.trace()
@cross_origin(origins=allowedorigins())
@auth.require
def post(requesttype, requestid, documentid):
try:
requestjson = request.get_json()
documentschema = ReclassifyDocumentSchema().load(requestjson)
activedocuments = documentservice().getactiverequestdocuments(requestid, requesttype)
documentpath = 'no documentpath found'
if (requesttype == 'ministryrequest'):
for document in activedocuments:
if document['foiministrydocumentid'] == int(documentid):
documentpath = document['documentpath']
else:
for document in activedocuments:
if document['foidocumentid'] == int(documentid):
documentpath = document['documentpath']

# move document in S3
moveresult = documentservice().copyrequestdocumenttonewlocation(documentschema['category'], documentpath)
# save new version of document with updated documentpath
if moveresult['status'] == 'success':
result = documentservice().createrequestdocumentversion(requestid, documentid, documentschema, AuthHelper.getuserid(), requesttype)
return {'status': result.success, 'message':result.message,'id':result.identifier} , 200
return {'status': False, 'message': "Something went wrong moving the document's location" }, 500
except ValidationError as err:
return {'status': False, 'message': str(err)}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

@cors_preflight('POST,OPTIONS')
@API.route('/foidocument/<requesttype>/<requestid>/documentid/<documentid>/replace')
class ReplaceFOIDocument(Resource):
Expand All @@ -120,9 +159,9 @@ def post(requestid, documentid, requesttype):
result = documentservice().createrequestdocumentversion(requestid, documentid, documentschema, AuthHelper.getuserid(), requesttype)
return {'status': result.success, 'message':result.message,'id':result.identifier} , 200
except ValidationError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
return {'status': False, 'message': str(err)}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -141,7 +180,7 @@ def post(requestid, documentid, requesttype):
try:
result = documentservice().deleterequestdocument(requestid, documentid, AuthHelper.getuserid(), requesttype)
return {'status': result.success, 'message':result.message,'id':result.identifier} , 200
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500
13 changes: 7 additions & 6 deletions request-management-api/request_api/resources/foiemail.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

API = Namespace('FOIEmail', description='Endpoints for FOI EMAIL management')
TRACER = Tracer.get_instance()
CUSTOM_KEYERROR_MESSAGE = "Key error has occured: "

@cors_preflight('POST,OPTIONS')
@API.route('/foiemail/<requestid>/ministryrequest/<ministryrequestid>/<servicename>')
Expand All @@ -50,9 +51,9 @@ def post(requestid, ministryrequestid, servicename):
result = emailservice().send(servicename.upper(), requestid, ministryrequestid, emailschema)
return json.dumps(result), 200 if result["success"] == True else 500
except ValueError as err:
return {'status': 500, 'message':err.messages}, 500
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
return {'status': 500, 'message': str(err)}, 500
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Expand All @@ -71,9 +72,9 @@ def post(requestid, ministryrequestid, servicename):
result = emailservice().acknowledge(servicename.upper(), requestid, ministryrequestid)
return json.dumps(result), 200 if result["success"] == True else 500
except ValueError as err:
return {'status': 500, 'message':err.messages}, 500
except KeyError as err:
return {'status': False, 'message':err.messages}, 400
return {'status': 500, 'message': str(err)}, 500
except KeyError as error:
return {'status': False, 'message': CUSTOM_KEYERROR_MESSAGE + str(error)}, 400
except BusinessException as exception:
return {'status': exception.status_code, 'message':exception.message}, 500

Loading

0 comments on commit 3ada012

Please sign in to comment.