2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2021-03-12 12:48:00 +00:00
|
|
|
#include "mtproto/mtproto_response.h"
|
2019-11-18 09:28:14 +00:00
|
|
|
|
|
|
|
#include <QtCore/QRegularExpression>
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2021-03-12 12:48:00 +00:00
|
|
|
namespace MTP {
|
2021-03-12 10:45:13 +00:00
|
|
|
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.");
|
2021-03-12 10:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // 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 10:45:13 +00:00
|
|
|
|
2021-03-12 12:48:00 +00:00
|
|
|
Error::Error(const mtpBuffer &reply) : Error(ParseError(reply)) {
|
2021-03-12 10:45:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 12:48:00 +00:00
|
|
|
int32 Error::code() const {
|
2021-03-12 10:45:13 +00:00
|
|
|
return _code;
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:48:00 +00:00
|
|
|
const QString &Error::type() const {
|
2021-03-12 10:45:13 +00:00
|
|
|
return _type;
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:48:00 +00:00
|
|
|
const QString &Error::description() const {
|
2021-03-12 10:45:13 +00:00
|
|
|
return _description;
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:48:00 +00:00
|
|
|
MTPrpcError Error::MTPLocal(
|
2021-03-12 10:45:13 +00:00
|
|
|
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(
|
2021-03-12 10:45:13 +00:00
|
|
|
const QString &type,
|
|
|
|
const QString &description) {
|
2021-03-12 12:48:00 +00:00
|
|
|
return Error(MTPLocal(type, description));
|
2021-03-12 10:45:13 +00:00
|
|
|
}
|
2021-03-12 12:48:00 +00:00
|
|
|
|
|
|
|
} // namespace MTP
|