From 1b4c39fe23d264f21888365032d0fea72e3a94c6 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 1 Sep 2019 15:25:28 -0300 Subject: [PATCH] errors: check args[0] for integer values and convert to ERR_MSG_MAP - channels: s/weeks/days - schemas: handle reqjson being None on validate() --- litecord/blueprints/channels.py | 2 +- litecord/errors.py | 20 +++++++++++++------- litecord/schemas.py | 7 +++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/litecord/blueprints/channels.py b/litecord/blueprints/channels.py index 2c13a39..d53cbeb 100644 --- a/litecord/blueprints/channels.py +++ b/litecord/blueprints/channels.py @@ -687,7 +687,7 @@ async def bulk_delete(channel_id: int): message_dt = snowflake_datetime(message_id) delta = datetime.datetime.utcnow() - message_dt - if delta.weeks > 2: + if delta.days > 14: raise BadRequest(50034) payload = { diff --git a/litecord/errors.py b/litecord/errors.py index 76f937c..c171b15 100644 --- a/litecord/errors.py +++ b/litecord/errors.py @@ -74,18 +74,24 @@ class LitecordError(Exception): """Base class for litecord errors""" status_code = 500 + def _get_err_msg(self, err_code: int) -> str: + if err_code is not None: + return ERR_MSG_MAP.get(err_code) or self.args[0] + + return repr(self) + @property def message(self) -> str: """Get an error's message string.""" try: - return self.args[0] + message = self.args[0] + + if isinstance(message, int): + return self._get_err_msg(message) + + return message except IndexError: - err_code = getattr(self, 'error_code', None) - - if err_code is not None: - return ERR_MSG_MAP.get(err_code) or self.args[0] - - return repr(self) + return self._get_err_msg(getattr(self, 'error_code', None)) @property def json(self): diff --git a/litecord/schemas.py b/litecord/schemas.py index de5aa21..bc9cce7 100644 --- a/litecord/schemas.py +++ b/litecord/schemas.py @@ -18,7 +18,7 @@ along with this program. If not, see . """ import re -from typing import Union, Dict, List +from typing import Union, Dict, List, Optional from cerberus import Validator from logbook import Logger @@ -158,7 +158,7 @@ class LitecordValidator(Validator): return isinstance(value, str) and (len(value) < 32) -def validate(reqjson: Union[Dict, List], schema: Dict, +def validate(reqjson: Optional[Union[Dict, List]], schema: Dict, raise_err: bool = True) -> Dict: """Validate the given user-given data against a schema, giving the "correct" version of the document, with all defaults applied. @@ -175,6 +175,9 @@ def validate(reqjson: Union[Dict, List], schema: Dict, """ validator = LitecordValidator(schema) + if reqjson is None: + raise BadRequest('No JSON provided') + try: valid = validator.validate(reqjson) except Exception: