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)
delta = datetime.datetime.utcnow() - message_dt
if delta.weeks > 2:
if delta.days > 14:
raise BadRequest(50034)
payload = {

View File

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

View File

@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
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: