Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test(request, commit):
global RUNNING
if RUNNING:
raise InvalidUsage('Please wait for the current test to finish')
try:
RUNNING = True
run_data = await git.test_commit(commit)
finally:
RUNNING = False
return response.json(run_data)
def handler_1(request):
raise InvalidUsage("OK")
result = {}
for name, field in serializer.fields.items():
if name in data:
if isinstance(field, serializers.ListField):
values = []
for i in data[name]:
i = i.strip()
# 每项是json结构
if isinstance(field.field, serializers.SerializerField):
try:
if isinstance(i, str):
values.append(json.loads(i.strip('"')))
else:
values.append(field.field.to_native(i))
except Exception:
raise exceptions.InvalidUsage(f'"{field.label}"的参数有误, data: {i}, data type: {type(i)}')
# 每项是普通值, 注意处理'"ITEM"'格式的首尾"号
else:
values.extend([j.strip('"') for j in i.split(',')]) # match `?a=b,c,d&a=d`
result[name] = []
for i in values:
if i not in result[name]:
result[name].append(i)
else:
result[name] = data[name][0]
return result
async def raise_exception(request, *args, **kwargs):
"""
测试一个异常
"""
raise exceptions.InvalidUsage('抛出的异常信息')
def ensure_sequence(self, value):
if value is None:
return []
if isinstance(value, list):
return value
elif isinstance(value, (str, Mapping)): # unacceptable iterables
pass
elif isinstance(value, Sequence):
return value
elif isinstance(value, Iterable):
return value
raise exceptions.InvalidUsage('内容应是列表')
def validate_ID(self, value, context=None):
if not IDField.ID_REGEX.match(value):
raise exceptions.InvalidUsage(self.messages['ID'].format(self.label, value))
def to_primitive(self, value, context=None):
data = []
value = self._coerce(value)
for index, item in enumerate(value):
try:
item_data = self.field.to_primitive(item, context)
if item_data:
data.append(item_data)
except exceptions.SanicException as exc:
raise exceptions.InvalidUsage('{0}的第{1}值有误:{2}'.format(self.label, index, exc))
return data
def _get_connection(request, id, create_if_not_made):
if 'uid' not in request.json:
raise InvalidUsage('Requires "uid" field in JSON body')
source = request['session'].uid_to_block(request.json['uid'])
if source is None:
raise InvalidUsage('No such item "%s"' % request.json['uid'])
connection = _get_mixer(request, id).connection_for_source(source, create_if_not_made=create_if_not_made)
if not connection and create_if_not_made is True:
raise InvalidUsage('Unable to connect "%s" to mixer %d' % (request.json['uid'], id))
return connection, request.json
def validate_ID(self, value):
if not IDField.ID_REGEX.match(value):
raise exceptions.InvalidUsage(self.messages['ID'].format(self.label, value))