//// Copyright (c) 2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)//// Distributed under the Boost Software License, Version 1.0. (See accompanying// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)//#ifndef SERVERTECHCHAT_SERVER_INCLUDE_ERROR_HPP#define SERVERTECHCHAT_SERVER_INCLUDE_ERROR_HPP#include <boost/assert/source_location.hpp>#include <boost/system/error_category.hpp>#include <boost/system/error_code.hpp>#include <boost/system/result.hpp>#include <string>#include <string_view>#include <utility>// Error management infrastructure. Uses Boost.System error codes and categories.// This is consistent with Asio, Beast and Redis.namespace chat {// Type alias to reduce verbosity.using error_code = boost::system::error_code;// Type alias to reduce verbosity.template <class T>using result = boost::system::result<T>;// An error code with a diagnostics messagestruct error_with_message{error_code ec;std::string msg;};// Required by the Boost.System infrastructure.// Throws an exception from an error_with_message.[[noreturn]] void throw_exception_from_error(const error_with_message& e, const boost::source_location&);// Like result, but the error state includes an error messagetemplate <class T>using result_with_message = boost::system::result<T, error_with_message>;// Error code enum for errors originated within our applicationenum class errc{redis_parse_error = 1, // Data retrieved from Redis didn't match the format we expectedredis_command_failed, // A Redis command failed execution (e.g. we provided the wrong number of args)websocket_parse_error, // Data received from the client didn't match the format we expectedusername_exists, // couldn't create user, duplicate usernameemail_exists, // couldn't create user, duplicate usernamenot_found, // couldn't retrieve a certain resource, it doesn't existinvalid_password_hash, // we found a password hash that was malformedalready_exists, // an entity can't be created because it already existsrequires_auth, // the requested resource requires authentication, but credentials haven't been provided// or are invalidinvalid_base64, // attempt to decode an invalid base64 stringuncaught_exception, // an API handler threw an unexpected exceptioninvalid_content_type, // an endpoint received an unsupported Content-Type};// The error category for errcconst boost::system::error_category& get_chat_category() noexcept;// Allows constructing error_code from errcinline error_code make_error_code(errc v) noexcept{return error_code(static_cast<int>(v), get_chat_category());}// Logs ec to stderrvoid log_error(error_code ec, std::string_view what, std::string_view diagnostics = "");inline void log_error(const error_with_message& err, std::string_view what){log_error(err.ec, what, err.msg);}} // namespace chat// Allows constructing error_code from errcnamespace boost {namespace system {template <>struct is_error_code_enum<chat::errc>{static constexpr bool value = true;};} // namespace system} // namespace boost// Returns an error_code with source-code location information on it#define CHAT_RETURN_ERROR(e) \{ \static constexpr auto loc = BOOST_CURRENT_LOCATION; \return ::chat::error_code(::chat::error_code(e), &loc); \}// Returns an error_with_message with source-code location information on the error_code#define CHAT_RETURN_ERROR_WITH_MESSAGE(e, msg) \{ \static constexpr auto loc = BOOST_CURRENT_LOCATION; \return ::chat::error_with_message{::chat::error_code(::chat::error_code(e), &loc), msg}; \}#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。