Refactor files to use human readable errors

This commit is contained in:
amcmanu3 2024-08-06 14:02:57 -04:00
parent f1fd57ea26
commit 2de9c90924

View File

@ -14,8 +14,17 @@ logger = logging.getLogger(__name__)
files_get_schema = {
"type": "object",
"properties": {
"page": {"type": "string", "minLength": 1},
"path": {"type": "string"},
"page": {
"type": "string",
"minLength": 1,
"error": "typeString",
"fill": True,
},
"path": {
"type": "string",
"error": "typeString",
"fill": True,
},
},
"additionalProperties": False,
"minProperties": 1,
@ -24,8 +33,16 @@ files_get_schema = {
files_patch_schema = {
"type": "object",
"properties": {
"path": {"type": "string"},
"contents": {"type": "string"},
"path": {
"type": "string",
"error": "typeString",
"fill": True,
},
"contents": {
"type": "string",
"error": "typeString",
"fill": True,
},
},
"additionalProperties": False,
"minProperties": 1,
@ -34,7 +51,11 @@ files_patch_schema = {
files_unzip_schema = {
"type": "object",
"properties": {
"folder": {"type": "string"},
"folder": {
"type": "string",
"error": "typeString",
"fill": True,
},
},
"additionalProperties": False,
"minProperties": 1,
@ -43,9 +64,21 @@ files_unzip_schema = {
files_create_schema = {
"type": "object",
"properties": {
"parent": {"type": "string"},
"name": {"type": "string"},
"directory": {"type": "boolean"},
"parent": {
"type": "string",
"error": "typeString",
"fill": True,
},
"name": {
"type": "string",
"error": "typeString",
"fill": True,
},
"directory": {
"type": "boolean",
"error": "typeBool",
"fill": True,
},
},
"additionalProperties": False,
"minProperties": 1,
@ -54,8 +87,16 @@ files_create_schema = {
files_rename_schema = {
"type": "object",
"properties": {
"path": {"type": "string"},
"new_name": {"type": "string"},
"path": {
"type": "string",
"error": "typeString",
"fill": True,
},
"new_name": {
"type": "string",
"error": "typeString",
"fill": True,
},
},
"additionalProperties": False,
"minProperties": 1,
@ -64,7 +105,12 @@ files_rename_schema = {
file_delete_schema = {
"type": "object",
"properties": {
"filename": {"type": "string", "minLength": 5},
"filename": {
"type": "string",
"minLength": 5,
"error": "typeString",
"fill": True,
},
},
"additionalProperties": False,
"minProperties": 1,
@ -229,13 +275,21 @@ class ApiServersServerFilesIndexHandler(BaseApiHandler):
)
try:
validate(data, file_delete_schema)
except ValidationError as e:
except ValidationError as why:
offending_key = None
if why.get("fill", None):
offending_key = why.path[0] if why.path else None
err = f"""{self.translator.translate(
"validators",
why.schema.get("error"),
self.controller.users.get_user_lang_by_id(auth_data[4]["user_id"]),
)} {offending_key}"""
return self.finish_json(
400,
{
"status": "error",
"error": "INVALID_JSON_SCHEMA",
"error_data": str(e),
"error_data": f"{str(err)}",
},
)
if not Helpers.validate_traversal(
@ -287,13 +341,21 @@ class ApiServersServerFilesIndexHandler(BaseApiHandler):
)
try:
validate(data, files_patch_schema)
except ValidationError as e:
except ValidationError as why:
offending_key = None
if why.get("fill", None):
offending_key = why.path[0] if why.path else None
err = f"""{self.translator.translate(
"validators",
why.schema.get("error"),
self.controller.users.get_user_lang_by_id(auth_data[4]["user_id"]),
)} {offending_key}"""
return self.finish_json(
400,
{
"status": "error",
"error": "INVALID_JSON_SCHEMA",
"error_data": str(e),
"error_data": f"{str(err)}",
},
)
if not Helpers.validate_traversal(
@ -341,13 +403,21 @@ class ApiServersServerFilesIndexHandler(BaseApiHandler):
)
try:
validate(data, files_create_schema)
except ValidationError as e:
except ValidationError as why:
offending_key = None
if why.get("fill", None):
offending_key = why.path[0] if why.path else None
err = f"""{self.translator.translate(
"validators",
why.schema.get("error"),
self.controller.users.get_user_lang_by_id(auth_data[4]["user_id"]),
)} {offending_key}"""
return self.finish_json(
400,
{
"status": "error",
"error": "INVALID_JSON_SCHEMA",
"error_data": str(e),
"error_data": f"{str(err)}",
},
)
path = os.path.join(data["parent"], data["name"])
@ -408,13 +478,21 @@ class ApiServersServerFilesCreateHandler(BaseApiHandler):
)
try:
validate(data, files_rename_schema)
except ValidationError as e:
except ValidationError as why:
offending_key = None
if why.get("fill", None):
offending_key = why.path[0] if why.path else None
err = f"""{self.translator.translate(
"validators",
why.schema.get("error"),
self.controller.users.get_user_lang_by_id(auth_data[4]["user_id"]),
)} {offending_key}"""
return self.finish_json(
400,
{
"status": "error",
"error": "INVALID_JSON_SCHEMA",
"error_data": str(e),
"error_data": f"{str(err)}",
},
)
path = data["path"]
@ -474,13 +552,21 @@ class ApiServersServerFilesCreateHandler(BaseApiHandler):
)
try:
validate(data, files_create_schema)
except ValidationError as e:
except ValidationError as why:
offending_key = None
if why.get("fill", None):
offending_key = why.path[0] if why.path else None
err = f"""{self.translator.translate(
"validators",
why.schema.get("error"),
self.controller.users.get_user_lang_by_id(auth_data[4]["user_id"]),
)} {offending_key}"""
return self.finish_json(
400,
{
"status": "error",
"error": "INVALID_JSON_SCHEMA",
"error_data": str(e),
"error_data": f"{str(err)}",
},
)
path = os.path.join(data["parent"], data["name"])
@ -541,13 +627,21 @@ class ApiServersServerFilesZipHandler(BaseApiHandler):
)
try:
validate(data, files_unzip_schema)
except ValidationError as e:
except ValidationError as why:
offending_key = None
if why.get("fill", None):
offending_key = why.path[0] if why.path else None
err = f"""{self.translator.translate(
"validators",
why.schema.get("error"),
self.controller.users.get_user_lang_by_id(auth_data[4]["user_id"]),
)} {offending_key}"""
return self.finish_json(
400,
{
"status": "error",
"error": "INVALID_JSON_SCHEMA",
"error_data": str(e),
"error_data": f"{str(err)}",
},
)
folder = data["folder"]