#ifndef BASE_INCLUDE_DEBUG_LYNX_ERROR_H_
#define BASE_INCLUDE_DEBUG_LYNX_ERROR_H_
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include "base/include/base_export.h"
#define LYNX_WARN(code, msg, suggestion) \
lynx::base::StoreError(code, msg, suggestion, \
lynx::base::LynxErrorLevel::Warn)
#define LYNX_ERROR(code, msg, suggestion) \
lynx::base::StoreError(code, msg, suggestion, \
lynx::base::LynxErrorLevel::Error)
#define LYNX_WARN_CHECK(expression, code, msg, suggestion) \
lynx::base::StoreErrorIfNot(expression, code, msg, suggestion, \
lynx::base::LynxErrorLevel::Warn)
#define LYNX_ERROR_CHECK(expression, code, msg, suggestion) \
lynx::base::StoreErrorIfNot(expression, code, msg, suggestion, \
lynx::base::LynxErrorLevel::Error)
namespace lynx {
namespace base {
static constexpr const char* kLynxErrorSuggestionRefOfficialSite =
"Please refer to the solution in Doc 'LynxError FAQ' on the official "
"website.";
enum class LynxErrorLevel : int32_t { Fatal = 0, Error, Warn };
BASE_EXPORT bool StoreError(int32_t error_code, std::string error_msg,
std::string fix_suggestion,
LynxErrorLevel level = LynxErrorLevel::Error);
BASE_EXPORT bool StoreErrorIfNot(bool expression, int32_t error_code,
std::string error_msg,
std::string fix_suggestion,
LynxErrorLevel level = LynxErrorLevel::Error);
struct BASE_EXPORT LynxError {
LynxError(int error_code, const char* format, ...);
LynxError(int error_code, std::string error_msg, std::string fix_suggestion,
LynxErrorLevel level, bool is_logbox_only = false);
LynxError(int error_code, std::string error_message)
: LynxError(error_code, error_message, "", LynxErrorLevel::Error) {}
LynxError(LynxError&& other) = default;
LynxError& operator=(LynxError&& other) = default;
LynxError(const LynxError& other) = delete;
LynxError& operator=(const LynxError& other) = delete;
void AddCallStack(const std::string& stack);
void AddContextInfo(const std::string& key, const std::string& value);
static std::string GetLevelString(int32_t level);
LynxErrorLevel error_level_;
int error_code_;
std::string error_message_;
std::string fix_suggestion_;
std::string root_cause_;
std::unordered_map<std::string, std::string> custom_info_;
bool is_logbox_only_;
bool should_abort_ = false;
};
class BASE_EXPORT ErrorStorage {
public:
static ErrorStorage& GetInstance();
template <typename... T>
bool SetError(T&&... args) {
if (error_ == nullptr) {
error_ = std::make_unique<LynxError>(std::forward<T>(args)...);
return true;
}
return false;
}
void Reset() { error_ = nullptr; }
const std::unique_ptr<LynxError>& GetError() const { return error_; }
void AddCustomInfoToError(
const std::unordered_map<std::string, std::string>& custom_info);
void AddCustomInfoToError(const std::string& key, const std::string& value);
private:
ErrorStorage() = default;
std::unique_ptr<LynxError> error_;
};
}
}
#endif