Include human readable errors in schedule edit/create

This commit is contained in:
amcmanu3 2024-08-06 14:54:03 -04:00
parent 2520e4b00f
commit ddfc13d9fd
3 changed files with 126 additions and 22 deletions

View File

@ -13,18 +13,32 @@ logger = logging.getLogger(__name__)
new_task_schema = { new_task_schema = {
"type": "object", "type": "object",
"properties": { "properties": {
"name": {"type": "string"}, "name": {
"type": "string",
"error": "typeString",
"fill": True,
},
"enabled": { "enabled": {
"type": "boolean", "type": "boolean",
"default": True, "default": True,
"error": "typeBool",
"fill": True,
}, },
"action": { "action": {
"type": "string", "type": "string",
"error": "typeString",
"fill": True,
}, },
"action_id": { "action_id": {
"type": "string", "type": "string",
"error": "typeString",
"fill": True,
},
"interval": {
"type": "integer",
"error": "typeInteger",
"fill": True,
}, },
"interval": {"type": "integer"},
"interval_type": { "interval_type": {
"type": "string", "type": "string",
"enum": [ "enum": [
@ -37,13 +51,43 @@ new_task_schema = {
# CRON tasks: # 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, "additionalProperties": False,
"minProperties": 1, "minProperties": 1,
@ -68,13 +112,21 @@ class ApiServersServerTasksIndexHandler(BaseApiHandler):
try: try:
validate(data, new_task_schema) 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( return self.finish_json(
400, 400,
{ {
"status": "error", "status": "error",
"error": "INVALID_JSON_SCHEMA", "error": "INVALID_JSON_SCHEMA",
"error_data": str(e), "error_data": f"{str(err)}",
}, },
) )

View File

@ -18,14 +18,24 @@ task_patch_schema = {
"enabled": { "enabled": {
"type": "boolean", "type": "boolean",
"default": True, "default": True,
"error": "typeBool",
"fill": True,
}, },
"action": { "action": {
"type": "string", "type": "string",
"error": "typeString",
"fill": True,
}, },
"action_id": { "action_id": {
"type": "string", "type": "string",
"error": "typeString",
"fill": True,
},
"interval": {
"type": "integer",
"error": "typeInteger",
"fill": True,
}, },
"interval": {"type": "integer"},
"interval_type": { "interval_type": {
"type": "string", "type": "string",
"enum": [ "enum": [
@ -38,14 +48,48 @@ task_patch_schema = {
# CRON tasks: # 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, "additionalProperties": False,
"minProperties": 1, "minProperties": 1,
@ -114,13 +158,21 @@ class ApiServersServerTasksTaskIndexHandler(BaseApiHandler):
try: try:
validate(data, task_patch_schema) 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( return self.finish_json(
400, 400,
{ {
"status": "error", "status": "error",
"error": "INVALID_JSON_SCHEMA", "error": "INVALID_JSON_SCHEMA",
"error_data": str(e), "error_data": f"{str(err)}",
}, },
) )

View File

@ -318,8 +318,8 @@
} else { } else {
bootbox.alert({ bootbox.alert({
title: responseData.status, title: responseData.error,
message: responseData.error message: responseData.error_data
}); });
} }
}); });