smartglocal_error.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 "smartglocal/smartglocal_error.h"
  8. namespace SmartGlocal {
  9. Error::Code Error::code() const {
  10. return _code;
  11. }
  12. QString Error::description() const {
  13. return _description;
  14. }
  15. QString Error::message() const {
  16. return _message;
  17. }
  18. QString Error::parameter() const {
  19. return _parameter;
  20. }
  21. Error Error::None() {
  22. return Error(Code::None, {}, {}, {});
  23. }
  24. Error Error::DecodedObjectFromResponse(QJsonObject object) {
  25. if (object.value("status").toString() == "ok") {
  26. return Error::None();
  27. }
  28. const auto entry = object.value("error");
  29. if (!entry.isObject()) {
  30. return {
  31. Code::Unknown,
  32. "GenericError",
  33. "Could not read the error response "
  34. "that was returned from SmartGlocal."
  35. };
  36. }
  37. const auto error = entry.toObject();
  38. const auto string = [&](QStringView key) {
  39. return error.value(key).toString();
  40. };
  41. const auto code = string(u"code");
  42. const auto description = string(u"description");
  43. // There should always be a message and type for the error
  44. if (code.isEmpty() || description.isEmpty()) {
  45. return {
  46. Code::Unknown,
  47. "GenericError",
  48. "Could not interpret the error response "
  49. "that was returned from SmartGlocal."
  50. };
  51. }
  52. return { Code::Unknown, code, description };
  53. }
  54. bool Error::empty() const {
  55. return (_code == Code::None);
  56. }
  57. } // namespace SmartGlocal