How to use the sanic.exceptions.InvalidUsage function in sanic

To help you get started, we’ve selected a few sanic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github channelcat / sanic-speedtest / main.py View on Github external
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)
github huge-success / sanic / tests / test_blueprints.py View on Github external
def handler_1(request):
        raise InvalidUsage("OK")
github songtao-git / wings-sanic / wings_sanic / views.py View on Github external
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
github songtao-git / wings-sanic / examples / sample / api / content / blog / views.py View on Github external
async def raise_exception(request, *args, **kwargs):
    """
    测试一个异常
    """
    raise exceptions.InvalidUsage('抛出的异常信息')
github songtao-git / wings-sanic / wings_sanic / serializers.py View on Github external
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('内容应是列表')
github songtao-git / wings-sanic / wings_sanic / serializers.py View on Github external
def validate_ID(self, value, context=None):
        if not IDField.ID_REGEX.match(value):
            raise exceptions.InvalidUsage(self.messages['ID'].format(self.label, value))
github songtao-git / wings-sanic / wings_sanic / serializers.py View on Github external
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
github bbc / brave / brave / api / route_handler.py View on Github external
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
github songtao-git / wings-sanic / src / schema / fields.py View on Github external
def validate_ID(self, value):
        if not IDField.ID_REGEX.match(value):
            raise exceptions.InvalidUsage(self.messages['ID'].format(self.label, value))