exception handling added for update API
""" Fledge package updater API support"""
+
+import json
from aiohttp import web
import datetime
@@ -45,13 +47,12 @@ async def update_package(request):
"""
create_message = "{} : a new schedule has been created".format(_FLEDGE_MANUAL_UPDATE_SCHEDULE)
-
status_message = "{} has been queued for execution".format(_FLEDGE_MANUAL_UPDATE_SCHEDULE)
-
error_message = "Failed to create the schedule {}".format(_FLEDGE_MANUAL_UPDATE_SCHEDULE)
+ schedule_disable_error_message = "{} schedule is disabled".format(_FLEDGE_MANUAL_UPDATE_SCHEDULE)
+ try:
task_found = False
-
# Get all the 'Scheduled Tasks'
schedule_list = await server.Server.scheduler.get_schedules()
@@ -59,9 +60,11 @@ async def update_package(request):
for schedule_info in schedule_list:
if schedule_info.name == _FLEDGE_MANUAL_UPDATE_SCHEDULE:
task_found = True
-
# Set the schedule id
schedule_id = schedule_info.schedule_id
+ if schedule_info.enabled is False:
+ _logger.warning(schedule_disable_error_message)
+ raise ValueError(schedule_disable_error_message)
break
# If no schedule then create it
@@ -70,10 +73,8 @@ async def update_package(request):
manual_schedule = ManualSchedule()
if not manual_schedule:
- # Return error
_logger.error(error_message)
- return web.json_response({"status": "Failed", "message": error_message})
-
+ raise ValueError(error_message)
# Set schedule fields
manual_schedule.name = _FLEDGE_MANUAL_UPDATE_SCHEDULE
manual_schedule.process_name = _FLEDGE_UPDATE_TASK
@@ -91,6 +92,11 @@ async def update_package(request):
# Add schedule_id to the schedule queue
await server.Server.scheduler.queue_task(schedule_id)
-
- # Return success
+ except ValueError as err:
+ msg = str(err)
+ raise web.HTTPBadRequest(reason=msg, body=json.dumps({"message": msg}))
+ except Exception as ex:
+ msg = str(ex)
+ raise web.HTTPInternalServerError(reason=msg, body=json.dumps({"message": msg}))
+ else:
return web.json_response({"status": "Running", "message": status_message})
