error_ut.cpp 3.2 KB

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