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 = {
"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)}",
},
)

View File

@ -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)}",
},
)

View File

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