errors: check args[0] for integer values and convert to ERR_MSG_MAP

- channels: s/weeks/days
 - schemas: handle reqjson being None on validate()
This commit is contained in:
Luna 2019-09-01 15:25:28 -03:00
parent 30d4c258d1
commit 1b4c39fe23
3 changed files with 19 additions and 10 deletions

View File

@ -687,7 +687,7 @@ async def bulk_delete(channel_id: int):
message_dt = snowflake_datetime(message_id) message_dt = snowflake_datetime(message_id)
delta = datetime.datetime.utcnow() - message_dt delta = datetime.datetime.utcnow() - message_dt
if delta.weeks > 2: if delta.days > 14:
raise BadRequest(50034) raise BadRequest(50034)
payload = { payload = {

View File

@ -74,18 +74,24 @@ class LitecordError(Exception):
"""Base class for litecord errors""" """Base class for litecord errors"""
status_code = 500 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 @property
def message(self) -> str: def message(self) -> str:
"""Get an error's message string.""" """Get an error's message string."""
try: try:
return self.args[0] message = self.args[0]
if isinstance(message, int):
return self._get_err_msg(message)
return message
except IndexError: except IndexError:
err_code = getattr(self, 'error_code', None) return self._get_err_msg(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)
@property @property
def json(self): def json(self):

View File

@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
import re import re
from typing import Union, Dict, List from typing import Union, Dict, List, Optional
from cerberus import Validator from cerberus import Validator
from logbook import Logger from logbook import Logger
@ -158,7 +158,7 @@ class LitecordValidator(Validator):
return isinstance(value, str) and (len(value) < 32) 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: raise_err: bool = True) -> Dict:
"""Validate the given user-given data against a schema, giving the """Validate the given user-given data against a schema, giving the
"correct" version of the document, with all defaults applied. "correct" version of the document, with all defaults applied.
@ -175,6 +175,9 @@ def validate(reqjson: Union[Dict, List], schema: Dict,
""" """
validator = LitecordValidator(schema) validator = LitecordValidator(schema)
if reqjson is None:
raise BadRequest('No JSON provided')
try: try:
valid = validator.validate(reqjson) valid = validator.validate(reqjson)
except Exception: except Exception: