/* * Modified by ScyllaDB * * Copyright (C) 2015-present ScyllaDB */ /* * SPDX-License-Identifier: (LicenseRef-ScyllaDB-Source-Available-1.0 and Apache-2.0) */ #pragma once #include "exceptions/exceptions.hh" #include #include namespace cql3 { namespace statements { /** * Utility methods use to perform request validation. */ namespace request_validations { template exceptions::invalid_request_exception invalid_request(fmt::format_string message_template, MessageArgs&&... message_args); /** * Checks that the specified expression is true. If not an InvalidRequestException will * be thrown. * * @param expression the expression to test * @param message_template the template used to build the error message * @param message_args the message arguments * @throws InvalidRequestException if the specified expression is false. */ template void check_true(bool expression, fmt::format_string message_template, MessageArgs&&... message_args) { if (!expression) { throw exceptions::invalid_request_exception(fmt::format(message_template, std::forward(message_args)...)); } } /** * Checks that the specified expression is false. If not an InvalidRequestException will * be thrown. * * @param expression the expression to test * @param message_template the template used to build the error message * @param message_args the message arguments * @throws InvalidRequestException if the specified expression is true. */ template void check_false(bool expression, fmt::format_string message_template, MessageArgs&&... message_args) { check_true(!expression, message_template, std::forward(message_args)...); } /** * Checks that the specified object is NOT null. * If it is an InvalidRequestException will be throws. * * @param object the object to test * @param message_template the template used to build the error message * @param message_args the message arguments * @return the object * @throws InvalidRequestException if the specified object is null. */ template T check_not_null(T object, fmt::format_string message_template, MessageArgs&... message_args) { check_true(bool(object), message_template, std::forward(message_args)...); return object; } /** * Returns an InvalidRequestException with the specified message. * * @param message_template the template used to build the error message * @param message_args the message arguments * @return an InvalidRequestException with the specified message. */ template exceptions::invalid_request_exception invalid_request(fmt::format_string message_template, MessageArgs&&... message_args) { return exceptions::invalid_request_exception(fmt::format(message_template, std::forward(message_args)...)); } } } }