def test_add_error_entries_to_json_body(self): from adhocracy_core.interfaces import error_entry error_entries = [error_entry('header', 'a', 'b')] inst = self.make_one(error_entries) assert inst.json_body['errors'] == [{'location': 'header', 'name': 'a', 'description': 'b'}]
def wrapped_view(context, request): token_is_set = UserTokenHeader in request.headers authenticated_is_empty = request.authenticated_userid is None if token_is_set and authenticated_is_empty: error = error_entry('header', 'X-User-Token', 'Invalid user token') request.errors.append(error) raise HTTPBadRequest() return view(context, request)
def wrapped_view(context, request): token_is_set = UserTokenHeader in request.headers authenticated_is_empty = request.authenticated_userid is None if token_is_set and authenticated_is_empty: error = error_entry('header', UserTokenHeader, 'Invalid user token') request.errors.append(error) raise HTTPBadRequest() return view(context, request)
def test_return_json_error_with_error_listing(self, error, request_): from adhocracy_core.interfaces import error_entry request_.errors = [error_entry('b', '', '')] inst = self.call_fut(error, request_) assert inst.content_type == 'application/json' assert inst.json ==\ {"errors": [{"location":"b","name":"","description":""}], "status": "error"} assert inst.code == 400
def handle_error_500_exception(error, request): """Return 500 JSON error.""" logger.exception('internal') description = '{}; time: {}'.format(exception_to_str(error), log_compatible_datetime()) error_entries = [error_entry('internal', '', description)] return JSONHTTPClientError(error_entries, request=request, code=500, title='Internal Server Error')
def handle_error_400_url_decode_error(error, request): """ Handle error thrown by Pyramid if the request path is not valid UTF-8. E.g. "/fooba%E9r/". """ error_entries = [error_entry('url', '', '400 Bad Request ' + str(error))] return JSONHTTPClientError(error_entries)
def wrapped_view(context, request): password_is_set = has_password_header(request) error = None if password_is_set: content = request.registry.content user = request.user password = get_header_password(request) is_valid = user.is_password_valid(request.registry, password) if not is_valid: error = error_entry('header', UserPasswordHeader, 'Invalid password') is_required_by_some_sheets = \ content.is_password_required(context, request) if not is_required_by_some_sheets: error = error_entry('header', UserPasswordHeader, 'Password not required') if error: request.errors.append(error) raise HTTPBadRequest() return view(context, request)
def handle_error_40x_exception(error, request): """Return JSON error for generic HTTPClientErrors. If `error` is :class:`JSONHTTPClientError` it is return without modifications. """ if isinstance(error, JSONHTTPClientError): return error error_entries = [error_entry('url', request.method, '{0} {1}'.format(error.status, error))] json_error = JSONHTTPClientError(error_entries, request=request, code=error.code, title=error.title) return json_error
def wrapped_view(context, request): has_anonymize_header = is_marked_anonymize(request) content = request.registry.content if has_anonymize_header: if request.method == 'POST': if request.path == '/batch' or request.path == '/api/batch': allowed = True else: allowed = content.can_add_anonymized(context, request) elif request.method == 'PUT': allowed = content.can_edit_anonymized(context, request) elif request.method == 'DELETE': allowed = content.can_delete_anonymized(context, request) else: # for other methods the header makes no sense, # we ignore it to simplify the frontend code allowed = True if not allowed: error = error_entry('header', AnonymizeHeader, 'Anonymize header not allowed') request.errors.append(error) raise HTTPBadRequest() return view(context, request)
def handle_error_400_colander_invalid(invalid, request): """Return JSON error for colander.Invalid errors.""" errors = [error_entry('body', n, d) for n, d in invalid.asdict().items()] return JSONHTTPClientError(errors, request=request)