// GCC 15 reports a false -Wmaybe-uninitialized on moved std::variant values (ttgui::json::Value); silenced for this file only.
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#include "mologram/tapi/ErrorMapper.hpp"
#include "tapi/cancel.hpp"
#include "tapi/errors.hpp"
namespace mologram {
bool ErrorMapper::isPremiumRequired(const std::string& t) {
if (t.rfind("FLOOD_", 0) == 0) return false;
if (t == "PREMIUM_CURRENTLY_UNAVAILABLE") return false;
return t.rfind("PREMIUM_", 0) == 0 || t.find("_PREMIUM_") != std::string::npos || t.find("PREMIUM_REQUIRED") != std::string::npos;
}
std::string ErrorMapper::scrub(std::string text, const std::vector<std::string>& secrets) {
for (const auto& s : secrets) {
if (s.size() < 4) continue; // a 1-3 character "secret" would mangle ordinary text
size_t pos = 0;
while ((pos = text.find(s, pos)) != std::string::npos) { text.replace(pos, s.size(), "<redacted>"); pos += 10; }
}
return text;
}
RpcError ErrorMapper::map(const std::exception& e, const std::vector<std::string>& secrets) {
if (dynamic_cast<const tapi::OperationCancelledError*>(&e)) return {ErrorCode::Cancelled, "cancelled"};
if (auto* pn = dynamic_cast<const PeerNotFound*>(&e)) return {ErrorCode::RpcError, scrub(pn->what(), secrets)};
if (auto* ni = dynamic_cast<const NotImplementedHere*>(&e)) return {ErrorCode::RpcError, scrub(ni->what(), secrets)};
if (auto* te = dynamic_cast<const tapi::errors::Error*>(&e)) {
const std::string& m = te->rpc_message();
std::string text = scrub(m.empty() ? std::string(te->what()) : m, secrets);
if (dynamic_cast<const tapi::errors::PremiumAccountRequiredError*>(te) || isPremiumRequired(m)) return {ErrorCode::PremiumRequired, text};
if (dynamic_cast<const tapi::errors::UnauthorizedError*>(te) && m != "SESSION_PASSWORD_NEEDED" && m.rfind("PHONE_CODE", 0) != 0 &&
m.rfind("PASSWORD", 0) != 0)
return {ErrorCode::NotLoggedIn, text};
return {ErrorCode::RpcError, text};
}
return {ErrorCode::Internal, scrub(e.what(), secrets)};
}
} // namespace mologram