How to use the cornice.errors.Errors 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 Cornices / cornice / tests / test_validation.py View on Github external
response = app.post('/service', params=json.dumps('buh'))

        expected = json.dumps({'body': '"buh"'}).encode('ascii')
        self.assertEqual(response.body, expected)

        app.get('/service?paid=yup')

        # valid = foo is one
        response = app.get('/service?foo=1&paid=yup')
        self.assertEqual(response.json['foo'], 1)

        # invalid value for foo
        response = app.get('/service?foo=buh&paid=yup', status=400)

        # check that json is returned
        errors = Errors.from_json(response.body)
        self.assertEqual(len(errors), 1)
github Kinto / kinto / kinto / core / testing.py View on Github external
def __init__(self, *args, **kwargs):
        super(DummyRequest, self).__init__(*args, **kwargs)
        self.upath_info = '/v0/'
        self.registry = mock.MagicMock(settings=DEFAULT_SETTINGS.copy())
        self.registry.id_generators = defaultdict(generators.UUID4)
        self.GET = {}
        self.headers = {}
        self.errors = cornice_errors.Errors(request=self)
        self.authenticated_userid = 'bob'
        self.authn_type = 'basicauth'
        self.prefixed_userid = 'basicauth:bob'
        self.json = {}
        self.validated = {}
        self.matchdict = {}
        self.response = mock.MagicMock(headers={})

        def route_url(*a, **kw):
            # XXX: refactor DummyRequest to take advantage of `pyramid.testing`
            parts = parse_url_overrides(kw)
            return ''.join([p for p in parts if p])

        self.route_url = route_url
github Cornices / cornice / tests / test_colander.py View on Github external
self.method = method
                self.url = 'http://example.com/path?ok=1'
                self.path = '/path'
                self.matchdict = {}
                self.body = body
                self.json_body = json_body
                self.GET = get or {}
                self.POST = {}
                self.validated = {}
                self.cookies = {}
                self.registry = object()
                self.content_type = 'application/json'
                self.localizer = MockTranslator()

        dummy_request = MockRequest(body, json_body, get)
        setattr(dummy_request, 'errors', Errors(dummy_request))
        return dummy_request
github Cornices / cornice / cornice / errors.py View on Github external
def from_list(cls, obj):
        """Transforms a python list into an `Errors` instance"""
        errors = Errors()
        for error in obj:
            errors.add(**error)
        return errors
github openprocurement / openprocurement.api / src / openprocurement / api / exceptions.py View on Github external
def __init__(self, error_code, error_entry):
        super(CorniceErrors, self).__init__(self)
        self.errors = Errors()
        self.errors.status = error_code
        self.errors.add(*error_entry)
github fedora-infra / bodhi / bodhi / server / views / generic.py View on Github external
Returns:
        bodhi.server.services.errors.json_handler: A pyramid.httpexceptions.HTTPError to be rendered
            to the user for the given exception.
    """
    errors = getattr(request, 'errors', [])
    status = getattr(exc, 'status_code', 500)

    if status not in (404, 403):
        log.exception("Error caught.  Handling JSON response.")
    else:
        log.warning(str(exc))

    if not len(errors):
        description = getattr(exc, 'explanation', None) or str(exc)

        errors = cornice.errors.Errors(status=status)
        errors.add('body', description=description, name=exc.__class__.__name__)
        request.errors = errors

    return bodhi.server.services.errors.json_handler(request)
github makinacorpus / ODE / ode / harvesting.py View on Github external
def __init__(self, text):
        self.text = text
        self.errors = Errors()
github fedora-infra / bodhi / bodhi / server / views / generic.py View on Github external
Returns:
        bodhi.server.services.errors.html_handler: A pyramid.httpexceptions.HTTPError to be rendered
            to the user for the given exception.
    """
    errors = getattr(request, 'errors', [])
    status = getattr(exc, 'status_code', 500)

    if status not in (404, 403):
        log.exception("Error caught.  Handling HTML response.")
    else:
        log.warning(str(exc))

    if not len(errors):
        description = getattr(exc, 'explanation', None) or str(exc)

        errors = cornice.errors.Errors(status=status)
        errors.add('body', description=description)
        request.errors = errors

    return bodhi.server.services.errors.html_handler(request)
github Cornices / cornice / cornice / errors.py View on Github external
def __init__(self, status=400, localizer=None):
        self.status = status
        self.localizer = localizer
        super(Errors, self).__init__()