From ddfc13d9fd9bac1a797e0b388211b47390c67ac4 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Tue, 6 Aug 2024 14:54:03 -0400 Subject: [PATCH] Include human readable errors in schedule edit/create --- .../routes/api/servers/server/tasks/index.py | 72 ++++++++++++++++--- .../api/servers/server/tasks/task/index.py | 72 ++++++++++++++++--- .../templates/panel/server_schedule_edit.html | 4 +- 3 files changed, 126 insertions(+), 22 deletions(-) diff --git a/app/classes/web/routes/api/servers/server/tasks/index.py b/app/classes/web/routes/api/servers/server/tasks/index.py index ed8b9df9..eba09d8b 100644 --- a/app/classes/web/routes/api/servers/server/tasks/index.py +++ b/app/classes/web/routes/api/servers/server/tasks/index.py @@ -13,18 +13,32 @@ logger = logging.getLogger(__name__) new_task_schema = { "type": "object", "properties": { - "name": {"type": "string"}, + "name": { + "type": "string", + "error": "typeString", + "fill": True, + }, "enabled": { "type": "boolean", "default": True, + "error": "typeBool", + "fill": True, }, "action": { "type": "string", + "error": "typeString", + "fill": True, }, "action_id": { "type": "string", + "error": "typeString", + "fill": True, + }, + "interval": { + "type": "integer", + "error": "typeInteger", + "fill": True, }, - "interval": {"type": "integer"}, "interval_type": { "type": "string", "enum": [ @@ -37,13 +51,43 @@ new_task_schema = { # CRON tasks: "", ], + "error": "enumErr", + "fill": True, + }, + "start_time": { + "type": "string", + "pattern": r"\d{1,2}:\d{1,2}", + "error": "typeString", + "fill": True, + }, + "command": { + "type": ["string", "null"], + "error": "typeString", + "fill": True, + }, + "one_time": { + "type": "boolean", + "default": False, + "error": "typeBool", + "fill": True, + }, + "cron_string": { + "type": "string", + "default": "", + "error": "typeString", + "fill": True, + }, + "parent": { + "type": ["integer", "null"], + "error": "typeInteger", + "fill": True, + }, + "delay": { + "type": "integer", + "default": 0, + "error": "typeInteger", + "fill": True, }, - "start_time": {"type": "string", "pattern": r"\d{1,2}:\d{1,2}"}, - "command": {"type": ["string", "null"]}, - "one_time": {"type": "boolean", "default": False}, - "cron_string": {"type": "string", "default": ""}, - "parent": {"type": ["integer", "null"]}, - "delay": {"type": "integer", "default": 0}, }, "additionalProperties": False, "minProperties": 1, @@ -68,13 +112,21 @@ class ApiServersServerTasksIndexHandler(BaseApiHandler): try: validate(data, new_task_schema) - except ValidationError as e: + except ValidationError as why: + offending_key = "" + if why.schema.get("fill", None): + offending_key = why.path[0] if why.path else None + err = f"""{offending_key} {self.translator.translate( + "validators", + why.schema.get("error"), + self.controller.users.get_user_lang_by_id(auth_data[4]["user_id"]), + )} {why.schema.get("enum", "")}""" return self.finish_json( 400, { "status": "error", "error": "INVALID_JSON_SCHEMA", - "error_data": str(e), + "error_data": f"{str(err)}", }, ) diff --git a/app/classes/web/routes/api/servers/server/tasks/task/index.py b/app/classes/web/routes/api/servers/server/tasks/task/index.py index 05c8cee9..6a910a4a 100644 --- a/app/classes/web/routes/api/servers/server/tasks/task/index.py +++ b/app/classes/web/routes/api/servers/server/tasks/task/index.py @@ -18,14 +18,24 @@ task_patch_schema = { "enabled": { "type": "boolean", "default": True, + "error": "typeBool", + "fill": True, }, "action": { "type": "string", + "error": "typeString", + "fill": True, }, "action_id": { "type": "string", + "error": "typeString", + "fill": True, + }, + "interval": { + "type": "integer", + "error": "typeInteger", + "fill": True, }, - "interval": {"type": "integer"}, "interval_type": { "type": "string", "enum": [ @@ -38,14 +48,48 @@ task_patch_schema = { # CRON tasks: "", ], + "error": "enumErr", + "fill": True, + }, + "name": { + "type": "string", + "error": "typeString", + "fill": True, + }, + "start_time": { + "type": "string", + "pattern": r"\d{1,2}:\d{1,2}", + "error": "typeString", + "fill": True, + }, + "command": { + "type": ["string", "null"], + "error": "typeString", + "fill": True, + }, + "one_time": { + "type": "boolean", + "default": False, + "error": "typeBool", + "fill": True, + }, + "cron_string": { + "type": "string", + "default": "", + "error": "typeString", + "fill": True, + }, + "parent": { + "type": ["integer", "null"], + "error": "typeInteger", + "fill": True, + }, + "delay": { + "type": "integer", + "default": 0, + "error": "typeInteger", + "fill": True, }, - "name": {"type": "string"}, - "start_time": {"type": "string", "pattern": r"\d{1,2}:\d{1,2}"}, - "command": {"type": ["string", "null"]}, - "one_time": {"type": "boolean", "default": False}, - "cron_string": {"type": "string", "default": ""}, - "parent": {"type": ["integer", "null"]}, - "delay": {"type": "integer", "default": 0}, }, "additionalProperties": False, "minProperties": 1, @@ -114,13 +158,21 @@ class ApiServersServerTasksTaskIndexHandler(BaseApiHandler): try: validate(data, task_patch_schema) - except ValidationError as e: + except ValidationError as why: + offending_key = "" + if why.schema.get("fill", None): + offending_key = why.path[0] if why.path else None + err = f"""{offending_key} {self.translator.translate( + "validators", + why.schema.get("error"), + self.controller.users.get_user_lang_by_id(auth_data[4]["user_id"]), + )} {why.schema.get("enum", "")}""" return self.finish_json( 400, { "status": "error", "error": "INVALID_JSON_SCHEMA", - "error_data": str(e), + "error_data": f"{str(err)}", }, ) diff --git a/app/frontend/templates/panel/server_schedule_edit.html b/app/frontend/templates/panel/server_schedule_edit.html index 50b48b10..21826ac6 100644 --- a/app/frontend/templates/panel/server_schedule_edit.html +++ b/app/frontend/templates/panel/server_schedule_edit.html @@ -318,8 +318,8 @@ } else { bootbox.alert({ - title: responseData.status, - message: responseData.error + title: responseData.error, + message: responseData.error_data }); } });