error_ut.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <library/cpp/json/json_reader.h>
  3. #include <yt/cpp/mapreduce/interface/errors.h>
  4. #include <yt/cpp/mapreduce/common/helpers.h>
  5. #include <util/generic/set.h>
  6. using namespace NYT;
  7. template<>
  8. void Out<NYT::TNode>(IOutputStream& s, const NYT::TNode& node)
  9. {
  10. s << "TNode:" << NodeToYsonString(node);
  11. }
  12. TEST(TErrorTest, ParseJson)
  13. {
  14. // Scary real world error! Бу!
  15. const char* jsonText =
  16. R"""({)"""
  17. R"""("code":500,)"""
  18. R"""("message":"Error resolving path //home/user/link",)"""
  19. R"""("attributes":{)"""
  20. R"""("fid":18446484571700269066,)"""
  21. R"""("method":"Create",)"""
  22. R"""("tid":17558639495721339338,)"""
  23. R"""("datetime":"2017-04-07T13:38:56.474819Z",)"""
  24. R"""("pid":414529,)"""
  25. R"""("host":"build01-01g.yt.yandex.net"},)"""
  26. R"""("inner_errors":[{)"""
  27. R"""("code":1,)"""
  28. R"""("message":"Node //tt cannot have children",)"""
  29. R"""("attributes":{)"""
  30. R"""("fid":18446484571700269066,)"""
  31. R"""("tid":17558639495721339338,)"""
  32. R"""("datetime":"2017-04-07T13:38:56.474725Z",)"""
  33. R"""("pid":414529,)"""
  34. R"""("host":"build01-01g.yt.yandex.net"},)"""
  35. R"""("inner_errors":[]}]})""";
  36. NJson::TJsonValue jsonValue;
  37. ReadJsonFastTree(jsonText, &jsonValue, /*throwOnError=*/ true);
  38. TYtError error(jsonValue);
  39. EXPECT_EQ(error.GetCode(), 500);
  40. EXPECT_EQ(error.GetMessage(), R"""(Error resolving path //home/user/link)""");
  41. EXPECT_EQ(error.InnerErrors().size(), 1u);
  42. EXPECT_EQ(error.InnerErrors()[0].GetCode(), 1);
  43. EXPECT_EQ(error.HasAttributes(), true);
  44. EXPECT_EQ(error.GetAttributes().at("method"), TNode("Create"));
  45. EXPECT_EQ(error.GetAllErrorCodes(), TSet<int>({500, 1}));
  46. }
  47. TEST(TErrorTest, GetYsonText) {
  48. const char* jsonText =
  49. R"""({)"""
  50. R"""("code":500,)"""
  51. R"""("message":"outer error",)"""
  52. R"""("attributes":{)"""
  53. R"""("method":"Create",)"""
  54. R"""("pid":414529},)"""
  55. R"""("inner_errors":[{)"""
  56. R"""("code":1,)"""
  57. R"""("message":"inner error",)"""
  58. R"""("attributes":{},)"""
  59. R"""("inner_errors":[])"""
  60. R"""(}]})""";
  61. TYtError error;
  62. error.ParseFrom(jsonText);
  63. TString ysonText = error.GetYsonText();
  64. TYtError error2(NodeFromYsonString(ysonText));
  65. EXPECT_EQ(
  66. ysonText,
  67. R"""({"code"=500;"message"="outer error";"attributes"={"method"="Create";"pid"=414529};"inner_errors"=[{"code"=1;"message"="inner error"}]})""");
  68. EXPECT_EQ(error2.GetYsonText(), ysonText);
  69. }