mirror of https://gitlab.com/litecord/litecord.git
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:
parent
30d4c258d1
commit
1b4c39fe23
|
|
@ -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 = {
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Reference in New Issue