Human readable backup errors

This commit is contained in:
amcmanu3 2024-08-06 18:11:35 -04:00
parent ddfc13d9fd
commit 693cda4239
3 changed files with 187 additions and 39 deletions

View File

@ -14,7 +14,12 @@ logger = logging.getLogger(__name__)
BACKUP_SCHEMA = { BACKUP_SCHEMA = {
"type": "object", "type": "object",
"properties": { "properties": {
"filename": {"type": "string", "minLength": 5}, "filename": {
"type": "string",
"minLength": 5,
"error": "typeString",
"fill": True,
},
}, },
"additionalProperties": False, "additionalProperties": False,
"minProperties": 1, "minProperties": 1,
@ -22,14 +27,47 @@ BACKUP_SCHEMA = {
BACKUP_PATCH_SCHEMA = { BACKUP_PATCH_SCHEMA = {
"type": "object", "type": "object",
"properties": { "properties": {
"backup_name": {"type": "string", "minLength": 3}, "backup_name": {
"backup_location": {"type": "string", "minLength": 1}, "type": "string",
"max_backups": {"type": "integer"}, "minLength": 3,
"compress": {"type": "boolean"}, "error": "backupName",
"shutdown": {"type": "boolean"}, },
"before": {"type": "string"}, "backup_location": {
"after": {"type": "string"}, "type": "string",
"excluded_dirs": {"type": "array"}, "minLength": 1,
"error": "typeString",
"fill": True,
},
"max_backups": {
"type": "integer",
"error": "typeInteger",
"fill": True,
},
"compress": {
"type": "boolean",
"error": "typeBool",
"fill": True,
},
"shutdown": {
"type": "boolean",
"error": "typeBool",
"fill": True,
},
"before": {
"type": "string",
"error": "typeString",
"fill": True,
},
"after": {
"type": "string",
"error": "typeString",
"fill": True,
},
"excluded_dirs": {
"type": "array",
"error": "typeList",
"fill": True,
},
}, },
"additionalProperties": False, "additionalProperties": False,
"minProperties": 1, "minProperties": 1,
@ -38,13 +76,37 @@ BACKUP_PATCH_SCHEMA = {
BASIC_BACKUP_PATCH_SCHEMA = { BASIC_BACKUP_PATCH_SCHEMA = {
"type": "object", "type": "object",
"properties": { "properties": {
"backup_name": {"type": "string", "minLength": 3}, "backup_name": {"type": "string", "minLength": 3, "error": "backupName"},
"max_backups": {"type": "integer"}, "max_backups": {
"compress": {"type": "boolean"}, "type": "integer",
"shutdown": {"type": "boolean"}, "error": "typeInteger",
"before": {"type": "string"}, "fill": True,
"after": {"type": "string"}, },
"excluded_dirs": {"type": "array"}, "compress": {
"type": "boolean",
"error": "typeBool",
"fill": True,
},
"shutdown": {
"type": "boolean",
"error": "typeBool",
"fill": True,
},
"before": {
"type": "string",
"error": "typeString",
"fill": True,
},
"after": {
"type": "string",
"error": "typeString",
"fill": True,
},
"excluded_dirs": {
"type": "array",
"error": "typeList",
"fill": True,
},
}, },
"additionalProperties": False, "additionalProperties": False,
"minProperties": 1, "minProperties": 1,
@ -179,13 +241,21 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler):
) )
try: try:
validate(data, BACKUP_SCHEMA) validate(data, BACKUP_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)}",
}, },
) )
@ -314,13 +384,21 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler):
validate(data, BACKUP_PATCH_SCHEMA) validate(data, BACKUP_PATCH_SCHEMA)
else: else:
validate(data, BASIC_BACKUP_PATCH_SCHEMA) validate(data, BASIC_BACKUP_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)}",
}, },
) )
backup_conf = self.controller.management.get_backup_config(backup_id) backup_conf = self.controller.management.get_backup_config(backup_id)
@ -405,13 +483,21 @@ class ApiServersServerBackupsBackupFilesIndexHandler(BaseApiHandler):
) )
try: try:
validate(data, BACKUP_SCHEMA) validate(data, BACKUP_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)}",
}, },
) )
self.helper.validate_traversal( self.helper.validate_traversal(

View File

@ -11,14 +11,43 @@ logger = logging.getLogger(__name__)
backup_patch_schema = { backup_patch_schema = {
"type": "object", "type": "object",
"properties": { "properties": {
"backup_name": {"type": "string", "minLength": 3}, "backup_name": {"type": "string", "minLength": 3, "error": "backupName"},
"backup_location": {"type": "string", "minLength": 1}, "backup_location": {
"max_backups": {"type": "integer"}, "type": "string",
"compress": {"type": "boolean"}, "minLength": 1,
"shutdown": {"type": "boolean"}, "error": "typeString",
"before": {"type": "string"}, "fill": True,
"after": {"type": "string"}, },
"excluded_dirs": {"type": "array"}, "max_backups": {
"type": "integer",
"error": "typeInteger",
"fill": True,
},
"compress": {
"type": "boolean",
"error": "typeBool",
"fill": True,
},
"shutdown": {
"type": "boolean",
"error": "typeBool",
"fill": True,
},
"before": {
"type": "string",
"error": "typeString",
"fill": True,
},
"after": {
"type": "string",
"error": "typeString",
"fill": True,
},
"excluded_dirs": {
"type": "array",
"error": "typeList",
"fill": True,
},
}, },
"additionalProperties": False, "additionalProperties": False,
"minProperties": 1, "minProperties": 1,
@ -27,13 +56,37 @@ backup_patch_schema = {
basic_backup_patch_schema = { basic_backup_patch_schema = {
"type": "object", "type": "object",
"properties": { "properties": {
"backup_name": {"type": "string", "minLength": 3}, "backup_name": {"type": "string", "minLength": 3, "error": "backupName"},
"max_backups": {"type": "integer"}, "max_backups": {
"compress": {"type": "boolean"}, "type": "integer",
"shutdown": {"type": "boolean"}, "error": "typeInt",
"before": {"type": "string"}, "fill": True,
"after": {"type": "string"}, },
"excluded_dirs": {"type": "array"}, "compress": {
"type": "boolean",
"error": "typeBool",
"fill": True,
},
"shutdown": {
"type": "boolean",
"error": "typeBool",
"fill": True,
},
"before": {
"type": "string",
"error": "typeString",
"fill": True,
},
"after": {
"type": "string",
"error": "typeString",
"fill": True,
},
"excluded_dirs": {
"type": "array",
"error": "typeList",
"fill": True,
},
}, },
"additionalProperties": False, "additionalProperties": False,
"minProperties": 1, "minProperties": 1,
@ -76,13 +129,21 @@ class ApiServersServerBackupsIndexHandler(BaseApiHandler):
validate(data, backup_patch_schema) validate(data, backup_patch_schema)
else: else:
validate(data, basic_backup_patch_schema) validate(data, basic_backup_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)}",
}, },
) )
if server_id not in [str(x["server_id"]) for x in auth_data[0]]: if server_id not in [str(x["server_id"]) for x in auth_data[0]]:

View File

@ -668,6 +668,7 @@
"uses": "Number of uses allowed (-1==No Limit)" "uses": "Number of uses allowed (-1==No Limit)"
}, },
"validators": { "validators": {
"backupName": "Backup name must be a string and a minimum length of 3.",
"enumErr": "failed validating. Acceptable data includes: ", "enumErr": "failed validating. Acceptable data includes: ",
"filesPageLen": "length must be greater than 1 for property", "filesPageLen": "length must be greater than 1 for property",
"passLength": "Password Too Short. Minimum Length: 8", "passLength": "Password Too Short. Minimum Length: 8",