errors.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #pragma once
  2. ///
  3. /// @file yt/cpp/mapreduce/interface/errors.h
  4. ///
  5. /// Errors and exceptions emitted by library.
  6. #include "fwd.h"
  7. #include "common.h"
  8. #include <library/cpp/yson/node/node.h>
  9. #include <util/generic/bt_exception.h>
  10. #include <util/generic/yexception.h>
  11. #include <util/generic/string.h>
  12. #include <util/generic/vector.h>
  13. namespace NJson {
  14. class TJsonValue;
  15. } // namespace NJson
  16. namespace NYT {
  17. ////////////////////////////////////////////////////////////////////////////////
  18. ///
  19. /// @brief Error that is thrown when library detects invalid usage of API.
  20. ///
  21. /// For example trying to start operations on empty table list.
  22. class TApiUsageError
  23. : public TWithBackTrace<yexception>
  24. { };
  25. ///
  26. /// @brief Error that is thrown when request retries continues for too long.
  27. ///
  28. /// @see NYT::TRetryConfig
  29. /// @see NYT::IRetryConfigProvider
  30. class TRequestRetriesTimeout
  31. : public yexception
  32. { };
  33. ////////////////////////////////////////////////////////////////////////////////
  34. ///
  35. /// @brief Error returned by YT cluster.
  36. ///
  37. /// An object of this class describe error that happened on YT server.
  38. /// Internally each error is a tree. Each node of the tree contains:
  39. /// - integer error code;
  40. /// - text description of error;
  41. /// - attributes describing error context.
  42. ///
  43. /// To get text description of an error one should use
  44. /// @ref NYT::TYtError::ShortDescription or @ref NYT::TYtError::FullDescription
  45. ///
  46. /// To distinguish between error kinds @ref NYT::TYtError::ContainsErrorCode should be used.
  47. ///
  48. /// @see NYT::TErrorResponse
  49. /// @see NYT::TOperationFailedError
  50. class TYtError
  51. {
  52. public:
  53. /// Constructs error with NYT::NClusterErrorCodes::OK code and empty message.
  54. TYtError();
  55. /// Constructs error with NYT::NClusterErrorCodes::Generic code and given message.
  56. explicit TYtError(const TString& message);
  57. /// Constructs error with given code and given message.
  58. TYtError(int code, const TString& message);
  59. /// Construct error from json representation.
  60. TYtError(const ::NJson::TJsonValue& value);
  61. /// Construct error from TNode representation.
  62. TYtError(const TNode& value);
  63. ///
  64. /// @brief Check if error or any of inner errors has given error code.
  65. ///
  66. /// Use this method to distinguish kind of error.
  67. bool ContainsErrorCode(int code) const;
  68. ///
  69. /// @brief Get short description of error.
  70. ///
  71. /// Short description contain text description of error and all inner errors.
  72. /// It is human readable but misses some important information (error codes, error attributes).
  73. ///
  74. /// Usually it's better to use @ref NYT::TYtError::FullDescription to log errors.
  75. TString ShortDescription() const;
  76. ///
  77. /// @brief Get full description of error.
  78. ///
  79. /// Full description contains readable short description
  80. /// followed by text yson representation of error that contains error codes and attributes.
  81. TString FullDescription() const;
  82. ///
  83. /// @brief Get error code of the topmost error.
  84. ///
  85. /// @warning Do not use this method to distinguish between error kinds
  86. /// @ref NYT::TYtError::ContainsErrorCode should be used instead.
  87. int GetCode() const;
  88. ///
  89. /// @brief Get error text of the topmost error.
  90. ///
  91. /// @warning This method should not be used to log errors
  92. /// since text description of inner errors is going to be lost.
  93. /// @ref NYT::TYtError::FullDescription should be used instead.
  94. const TString& GetMessage() const;
  95. ///
  96. /// @brief Check if error or any of inner errors contains given text chunk.
  97. ///
  98. /// @warning @ref NYT::TYtError::ContainsErrorCode must be used instead of
  99. /// this method when possible. If there is no suitable error code it's
  100. /// better to ask yt@ to add one. This method should only be used as workaround.
  101. bool ContainsText(const TStringBuf& text) const;
  102. /// @brief Get inner errors.
  103. const TVector<TYtError>& InnerErrors() const;
  104. /// Parse error from json string.
  105. void ParseFrom(const TString& jsonError);
  106. /// Collect error codes from entire error tree.
  107. TSet<int> GetAllErrorCodes() const;
  108. /// Check if error has any attributes.
  109. bool HasAttributes() const;
  110. /// Get error attributes.
  111. const TNode::TMapType& GetAttributes() const;
  112. /// Get text yson representation of error
  113. TString GetYsonText() const;
  114. private:
  115. int Code_;
  116. TString Message_;
  117. TVector<TYtError> InnerErrors_;
  118. TNode::TMapType Attributes_;
  119. };
  120. ////////////////////////////////////////////////////////////////////////////////
  121. ///
  122. /// @brief Generic error response returned by server.
  123. ///
  124. /// TErrorResponse can be thrown from almost any client method when server responds with error.
  125. ///
  126. class TErrorResponse
  127. : public yexception
  128. {
  129. public:
  130. TErrorResponse(int httpCode, const TString& requestId);
  131. TErrorResponse(int httpCode, TYtError error);
  132. /// Get error object returned by server.
  133. const TYtError& GetError() const;
  134. /// Get if (correlation-id) of request that was responded with error.
  135. TString GetRequestId() const;
  136. /// Get HTTP code of response.
  137. int GetHttpCode() const;
  138. /// Is error parsed from response trailers.
  139. bool IsFromTrailers() const;
  140. /// Check if error was caused by transport problems inside YT cluster.
  141. bool IsTransportError() const;
  142. /// Check if error was caused by failure to resolve cypress path.
  143. bool IsResolveError() const;
  144. /// Check if error was caused by lack of permissions to execute request.
  145. bool IsAccessDenied() const;
  146. /// Check if error was caused by failure to lock object because of another transaction is holding lock.
  147. bool IsConcurrentTransactionLockConflict() const;
  148. /// Check if error was caused by request quota limit exceeding.
  149. bool IsRequestRateLimitExceeded() const;
  150. // YT can't serve request because it is overloaded.
  151. bool IsRequestQueueSizeLimitExceeded() const;
  152. /// Check if error was caused by failure to get chunk. Such errors are almost always temporary.
  153. bool IsChunkUnavailable() const;
  154. /// Check if error was caused by internal YT timeout.
  155. bool IsRequestTimedOut() const;
  156. /// Check if error was caused by trying to work with transaction that was finished or never existed.
  157. bool IsNoSuchTransaction() const;
  158. // User reached their limit of concurrently running operations.
  159. bool IsConcurrentOperationsLimitReached() const;
  160. /// @deprecated This method must not be used.
  161. bool IsOk() const;
  162. void SetRawError(const TString& message);
  163. void SetError(TYtError error);
  164. void ParseFromJsonError(const TString& jsonError);
  165. void SetIsFromTrailers(bool isFromTrailers);
  166. private:
  167. void Setup();
  168. private:
  169. int HttpCode_;
  170. TString RequestId_;
  171. TYtError Error_;
  172. bool IsFromTrailers_ = false;
  173. };
  174. ////////////////////////////////////////////////////////////////////////////////
  175. /// Info about failed jobs.
  176. ///
  177. /// @see NYT::TOperationFailedError
  178. struct TFailedJobInfo
  179. {
  180. /// Id of a job.
  181. TJobId JobId;
  182. /// Error describing job failure.
  183. TYtError Error;
  184. /// Stderr of job.
  185. ///
  186. /// @note YT doesn't store all job stderrs, check @ref NYT::IOperationClient::GetJobStderr
  187. /// for list of limitations.
  188. ///
  189. /// @see NYT::IOperationClient::GetJobStderr
  190. TString Stderr;
  191. };
  192. ////////////////////////////////////////////////////////////////////////////////
  193. ///
  194. /// @brief Error that is thrown when operation watched by library fails.
  195. ///
  196. /// This error is thrown from operation starting methods when they are started in sync mode (@ refNYT::TOperationOptions::Wait == true)
  197. /// or from future returned by NYT::IOperation::Watch.
  198. ///
  199. /// @see NYT::IOperationClient
  200. class TOperationFailedError
  201. : public yexception
  202. {
  203. public:
  204. /// Final state of operation.
  205. enum EState {
  206. /// Operation was failed due to some error.
  207. Failed,
  208. /// Operation didn't experienced errors, but was aborted by user request or by YT.
  209. Aborted,
  210. };
  211. public:
  212. TOperationFailedError(EState state, TOperationId id, TYtError ytError, TVector<TFailedJobInfo> failedJobInfo);
  213. /// Get final state of operation.
  214. EState GetState() const;
  215. /// Get operation id.
  216. TOperationId GetOperationId() const;
  217. /// Return operation error.
  218. const TYtError& GetError() const;
  219. /// Return info about failed jobs (if any).
  220. const TVector<TFailedJobInfo>& GetFailedJobInfo() const;
  221. private:
  222. EState State_;
  223. TOperationId OperationId_;
  224. TYtError Error_;
  225. TVector<TFailedJobInfo> FailedJobInfo_;
  226. };
  227. ////////////////////////////////////////////////////////////////////////////////
  228. } // namespace NYT