How to use the cornice.util.json_error function in cornice

To help you get started, we’ve selected a few cornice 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 liqd / adhocracy3 / src / adhocracy / adhocracy / rest / views / contents.py View on Github external
def validate_request_data(schema, request):
        """Validate request POST/GET data with colander schema.
           Add errors to request.errors
           Add validated data to request.validated
           Raises HTTError 400 if request.errors is not empty
        """
        schema = CorniceSchema(schema)
        validate_colander_schema(schema, request)
        if request.errors:
            raise json_error(request.errors)
github openprocurement / openprocurement.api / src / openprocurement / api / utils / common.py View on Github external
}
    if request_params:
        params['ROLE'] = str(request.authenticated_role)
        if request.params:
            params['PARAMS'] = str(dict(request.params))
    if request.matchdict:
        for x, j in request.matchdict.items():
            params[x.upper()] = j
    if 'tender' in request.validated:
        params['TENDER_REV'] = request.validated['tender'].rev
        params['TENDERID'] = request.validated['tender'].tenderID
        params['TENDER_STATUS'] = request.validated['tender'].status
    request.registry.notify(ErrorDesctiptorEvent(request, params))
    LOGGER.info('Error on processing request "{}"'.format(dumps(errors, indent=4)),
                extra=context_unpack(request, {'MESSAGE_ID': 'error_handler'}, params))
    return json_error(request)
github Cornices / cornice / cornice / service.py View on Github external
arguments[arg] = conf.pop(arg, getattr(self, arg, None))

        for arg in self.list_arguments:
            # rather than overwriting, extend the defined lists if
            # any. take care of re-creating the lists before appending
            # items to them, to avoid modifications to the already
            # existing ones
            value = list(getattr(self, arg, []))
            if arg in conf:
                value.extend(to_list(conf.pop(arg)))
            arguments[arg] = value

        # Allow custom error handler
        arguments['error_handler'] = conf.pop('error_handler',
                                              getattr(self, 'error_handler',
                                                      json_error))

        # exclude some validators or filters
        if 'exclude' in conf:
            for item in to_list(conf.pop('exclude')):
                for container in arguments['validators'], arguments['filters']:
                    if item in container:
                        container.remove(item)

        # also include the other key,value pair we don't know anything about
        arguments.update(conf)

        # if some keys have been defined service-wide, then we need to add
        # them to the returned dict.
        if hasattr(self, 'arguments'):
            for key, value in self.arguments.items():
                if key not in arguments:
github mozilla-services / tokenserver / tokenserver / tokenmgr.py View on Github external
version = request.matchdict.get('version')
        errors = Errors()

        if application not in self.applications:
            errors.add("uri", "application",
            "the application %r is not defined, please use one of %s" % (
                        application, ", ".join(self.applications.keys())))

        if version not in self.applications[application]:
            versions = self.applications[application]
            errors.add("uri", "version",
              ("the application %r is not defined for this version, please "
               "use one of %s") % (application, ", ".join(versions)))

        if len(errors) > 0:
            raise json_error(errors, 404)