tdesktop/Telegram/SourceFiles/mtproto/mtproto_response.cpp

82 lines
1.9 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
2021-03-12 12:48:00 +00:00
#include "mtproto/mtproto_response.h"
2019-11-18 09:28:14 +00:00
#include <QtCore/QRegularExpression>
2021-03-12 12:48:00 +00:00
namespace MTP {
namespace {
[[nodiscard]] MTPrpcError ParseError(const mtpBuffer &reply) {
auto result = MTPRpcError();
auto from = reply.constData();
return result.read(from, from + reply.size())
? result
2021-03-12 12:48:00 +00:00
: Error::MTPLocal("RESPONSE_PARSE_FAILED", "Error parse failed.");
}
} // namespace
2021-03-12 12:48:00 +00:00
Error::Error(const MTPrpcError &error)
2019-07-05 13:38:38 +00:00
: _code(error.c_rpc_error().verror_code().v) {
QString text = qs(error.c_rpc_error().verror_message());
2018-06-02 14:29:21 +00:00
if (_code < 0 || _code >= 500) {
2019-11-18 09:28:14 +00:00
_type = "INTERNAL_SERVER_ERROR";
2018-06-02 14:29:21 +00:00
_description = text;
} else {
const auto expression = QRegularExpression(
"^([A-Z0-9_]+)(: .*)?$",
2019-11-18 09:28:14 +00:00
(QRegularExpression::DotMatchesEverythingOption
| QRegularExpression::MultilineOption));
2018-06-02 14:29:21 +00:00
const auto match = expression.match(text);
if (match.hasMatch()) {
_type = match.captured(1);
_description = match.captured(2).mid(2);
} else {
2019-11-18 09:28:14 +00:00
_type = "CLIENT_BAD_RPC_ERROR";
_description = "Bad rpc error received, text = '" + text + '\'';
2018-06-02 14:29:21 +00:00
}
}
}
2021-03-12 12:48:00 +00:00
Error::Error(const mtpBuffer &reply) : Error(ParseError(reply)) {
}
2021-03-12 12:48:00 +00:00
int32 Error::code() const {
return _code;
}
2021-03-12 12:48:00 +00:00
const QString &Error::type() const {
return _type;
}
2021-03-12 12:48:00 +00:00
const QString &Error::description() const {
return _description;
}
2021-03-12 12:48:00 +00:00
MTPrpcError Error::MTPLocal(
const QString &type,
const QString &description) {
return MTP_rpc_error(
MTP_int(0),
MTP_bytes(
("CLIENT_"
+ type
+ (description.length()
? (": " + description)
: QString())).toUtf8()));
}
2021-03-12 12:48:00 +00:00
Error Error::Local(
const QString &type,
const QString &description) {
2021-03-12 12:48:00 +00:00
return Error(MTPLocal(type, description));
}
2021-03-12 12:48:00 +00:00
} // namespace MTP