mtproto_response.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "mtproto/mtproto_response.h"
  8. #include <QtCore/QRegularExpression>
  9. namespace MTP {
  10. namespace {
  11. [[nodiscard]] MTPrpcError ParseError(const mtpBuffer &reply) {
  12. auto result = MTPRpcError();
  13. auto from = reply.constData();
  14. return result.read(from, from + reply.size())
  15. ? result
  16. : Error::MTPLocal("RESPONSE_PARSE_FAILED", "Error parse failed.");
  17. }
  18. } // namespace
  19. Error::Error(const MTPrpcError &error)
  20. : _code(error.c_rpc_error().verror_code().v) {
  21. QString text = qs(error.c_rpc_error().verror_message());
  22. static const auto Expression = QRegularExpression(
  23. "^([A-Z0-9_]+)(: .*)?$",
  24. (QRegularExpression::DotMatchesEverythingOption
  25. | QRegularExpression::MultilineOption));
  26. const auto match = Expression.match(text);
  27. if (match.hasMatch()) {
  28. _type = match.captured(1);
  29. _description = match.captured(2).mid(2);
  30. } else if (_code < 0 || _code >= 500) {
  31. _type = "INTERNAL_SERVER_ERROR";
  32. _description = text;
  33. } else {
  34. _type = "CLIENT_BAD_RPC_ERROR";
  35. _description = "Bad rpc error received, text = '" + text + '\'';
  36. }
  37. }
  38. Error::Error(const mtpBuffer &reply) : Error(ParseError(reply)) {
  39. }
  40. int32 Error::code() const {
  41. return _code;
  42. }
  43. const QString &Error::type() const {
  44. return _type;
  45. }
  46. const QString &Error::description() const {
  47. return _description;
  48. }
  49. MTPrpcError Error::MTPLocal(
  50. const QString &type,
  51. const QString &description) {
  52. return MTP_rpc_error(
  53. MTP_int(0),
  54. MTP_bytes(
  55. ("CLIENT_"
  56. + type
  57. + (description.length()
  58. ? (": " + description)
  59. : QString())).toUtf8()));
  60. }
  61. Error Error::Local(
  62. const QString &type,
  63. const QString &description) {
  64. return Error(MTPLocal(type, description));
  65. }
  66. } // namespace MTP