errors.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <library/cpp/yson/node/node.h>
  8. #include <util/generic/bt_exception.h>
  9. #include <util/generic/guid.h>
  10. #include <util/generic/string.h>
  11. #include <util/generic/vector.h>
  12. #include <util/generic/yexception.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 from given parameters.
  58. TYtError(int code, TString message, TVector<TYtError> inner = {}, TNode::TMapType attributes = {});
  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(TYtError error, const TString& requestId);
  131. /// Get error object returned by server.
  132. const TYtError& GetError() const;
  133. /// Get if (correlation-id) of request that was responded with error.
  134. TString GetRequestId() const;
  135. /// Is error parsed from response trailers.
  136. bool IsFromTrailers() const;
  137. /// Check if error was caused by transport problems inside YT cluster.
  138. bool IsTransportError() const;
  139. /// Check if error was caused by failure to resolve cypress path.
  140. bool IsResolveError() const;
  141. /// Check if error was caused by lack of permissions to execute request.
  142. bool IsAccessDenied() const;
  143. /// Check if error was caused by authorization issues.
  144. bool IsUnauthorized() const;
  145. /// Check if error was caused by failure to lock object because of another transaction is holding lock.
  146. bool IsConcurrentTransactionLockConflict() const;
  147. /// Check if error was caused by request quota limit exceeding.
  148. bool IsRequestRateLimitExceeded() const;
  149. // YT can't serve request because it is overloaded.
  150. bool IsRequestQueueSizeLimitExceeded() const;
  151. /// Check if error was caused by failure to get chunk. Such errors are almost always temporary.
  152. bool IsChunkUnavailable() const;
  153. /// Check if error was caused by internal YT timeout.
  154. bool IsRequestTimedOut() const;
  155. /// Check if error was caused by trying to work with transaction that was finished or never existed.
  156. bool IsNoSuchTransaction() const;
  157. // User reached their limit of concurrently running operations.
  158. bool IsConcurrentOperationsLimitReached() const;
  159. /// @deprecated This method must not be used.
  160. bool IsOk() const;
  161. void SetRawError(const TString& message);
  162. void SetError(TYtError error);
  163. void ParseFromJsonError(const TString& jsonError);
  164. void SetIsFromTrailers(bool isFromTrailers);
  165. private:
  166. void Setup();
  167. private:
  168. TString RequestId_;
  169. TYtError Error_;
  170. bool IsFromTrailers_ = false;
  171. };
  172. ////////////////////////////////////////////////////////////////////////////////
  173. /// @brief System error indicating that response from server cannot be received
  174. class TTransportError
  175. : public yexception
  176. {
  177. public:
  178. explicit TTransportError(TYtError error);
  179. };
  180. ////////////////////////////////////////////////////////////////////////////////
  181. /// Info about failed jobs.
  182. ///
  183. /// @see NYT::TOperationFailedError
  184. struct TFailedJobInfo
  185. {
  186. /// Id of a job.
  187. TJobId JobId;
  188. /// Error describing job failure.
  189. TYtError Error;
  190. /// Stderr of job.
  191. ///
  192. /// @note YT doesn't store all job stderrs, check @ref NYT::IOperationClient::GetJobStderr
  193. /// for list of limitations.
  194. ///
  195. /// @see NYT::IOperationClient::GetJobStderr
  196. TString Stderr;
  197. };
  198. ////////////////////////////////////////////////////////////////////////////////
  199. ///
  200. /// @brief Error that is thrown when operation watched by library fails.
  201. ///
  202. /// This error is thrown from operation starting methods when they are started in sync mode (@ refNYT::TOperationOptions::Wait == true)
  203. /// or from future returned by NYT::IOperation::Watch.
  204. ///
  205. /// @see NYT::IOperationClient
  206. class TOperationFailedError
  207. : public yexception
  208. {
  209. public:
  210. /// Final state of operation.
  211. enum EState {
  212. /// Operation was failed due to some error.
  213. Failed,
  214. /// Operation didn't experienced errors, but was aborted by user request or by YT.
  215. Aborted,
  216. };
  217. public:
  218. TOperationFailedError(EState state, TOperationId id, TYtError ytError, TVector<TFailedJobInfo> failedJobInfo);
  219. /// Get final state of operation.
  220. EState GetState() const;
  221. /// Get operation id.
  222. TOperationId GetOperationId() const;
  223. /// Return operation error.
  224. const TYtError& GetError() const;
  225. /// Return info about failed jobs (if any).
  226. const TVector<TFailedJobInfo>& GetFailedJobInfo() const;
  227. private:
  228. EState State_;
  229. TOperationId OperationId_;
  230. TYtError Error_;
  231. TVector<TFailedJobInfo> FailedJobInfo_;
  232. };
  233. ////////////////////////////////////////////////////////////////////////////////
  234. } // namespace NYT