error.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include <string>
  3. #include <stdexcept>
  4. #include <optional>
  5. #include <variant>
  6. #include <nlohmann/json.hpp>
  7. namespace smartbotic::common {
  8. // Error codes
  9. enum class ErrorCode {
  10. // General errors (1xxx)
  11. Unknown = 1000,
  12. InvalidArgument = 1001,
  13. NotFound = 1002,
  14. AlreadyExists = 1003,
  15. PermissionDenied = 1004,
  16. Unauthenticated = 1005,
  17. ResourceExhausted = 1006,
  18. FailedPrecondition = 1007,
  19. Aborted = 1008,
  20. Internal = 1009,
  21. Unavailable = 1010,
  22. Timeout = 1011,
  23. // Database errors (2xxx)
  24. DatabaseError = 2000,
  25. DocumentNotFound = 2001,
  26. CollectionNotFound = 2002,
  27. VersionConflict = 2003,
  28. ValidationError = 2004,
  29. QueryError = 2005,
  30. // A write whose outcome nobody knows. From client 2.13.1 a DeadlineExceeded
  31. // on a write carries reason "outcome_unknown": the deadline can expire while
  32. // the server finishes the write, so it may well have been applied. This is
  33. // deliberately NOT Timeout - Timeout means "it did not happen, repeat it",
  34. // and repeating an insert or a delete here duplicates a row or reports a
  35. // false not-found.
  36. WriteOutcomeUnknown = 2006,
  37. // The stored document would be too large to ever read back (client 2.13.1,
  38. // reason "document_too_large"). Refused up front, including a patch whose
  39. // RESULT would grow the row past the limit. Permanent: retrying is useless.
  40. DocumentTooLarge = 2007,
  41. // Auth errors (3xxx)
  42. AuthError = 3000,
  43. InvalidCredentials = 3001,
  44. TokenExpired = 3002,
  45. TokenInvalid = 3003,
  46. SessionExpired = 3004,
  47. InsufficientPermissions = 3005,
  48. // Workflow errors (4xxx)
  49. WorkflowError = 4000,
  50. WorkflowNotFound = 4001,
  51. WorkflowInactive = 4002,
  52. NodeNotFound = 4003,
  53. NodeExecutionFailed = 4004,
  54. ExecutionTimeout = 4005,
  55. ExecutionCancelled = 4006,
  56. CircuitBreakerOpen = 4007,
  57. // Runner errors (5xxx)
  58. RunnerError = 5000,
  59. RunnerUnavailable = 5001,
  60. NoRunnersAvailable = 5002,
  61. ScriptError = 5003,
  62. ScriptTimeout = 5004,
  63. };
  64. // Convert error code to HTTP status
  65. int errorCodeToHttpStatus(ErrorCode code);
  66. // Error class with detailed information
  67. class Error {
  68. public:
  69. Error(ErrorCode code, std::string message);
  70. Error(ErrorCode code, std::string message, nlohmann::json details);
  71. ErrorCode code() const { return code_; }
  72. const std::string& message() const { return message_; }
  73. const nlohmann::json& details() const { return details_; }
  74. nlohmann::json toJson() const;
  75. std::string toString() const;
  76. static Error fromException(const std::exception& e);
  77. private:
  78. ErrorCode code_;
  79. std::string message_;
  80. nlohmann::json details_;
  81. };
  82. // Exception class that wraps Error
  83. class SmartBoticException : public std::runtime_error {
  84. public:
  85. explicit SmartBoticException(Error error);
  86. const Error& error() const { return error_; }
  87. ErrorCode code() const { return error_.code(); }
  88. private:
  89. Error error_;
  90. };
  91. // Result type for operations that can fail
  92. template<typename T>
  93. class Result {
  94. public:
  95. Result(T value) : data_(std::move(value)) {}
  96. Result(Error error) : data_(std::move(error)) {}
  97. bool ok() const { return std::holds_alternative<T>(data_); }
  98. bool failed() const { return !ok(); }
  99. const T& value() const { return std::get<T>(data_); }
  100. T& value() { return std::get<T>(data_); }
  101. const Error& error() const { return std::get<Error>(data_); }
  102. // Throw if error
  103. const T& valueOrThrow() const {
  104. if (failed()) {
  105. throw SmartBoticException(error());
  106. }
  107. return value();
  108. }
  109. T& valueOrThrow() {
  110. if (failed()) {
  111. throw SmartBoticException(error());
  112. }
  113. return value();
  114. }
  115. private:
  116. std::variant<T, Error> data_;
  117. };
  118. // Void result specialization
  119. template<>
  120. class Result<void> {
  121. public:
  122. Result() : error_(std::nullopt) {}
  123. Result(Error error) : error_(std::move(error)) {}
  124. bool ok() const { return !error_.has_value(); }
  125. bool failed() const { return error_.has_value(); }
  126. const Error& error() const { return *error_; }
  127. void throwIfFailed() const {
  128. if (failed()) {
  129. throw SmartBoticException(*error_);
  130. }
  131. }
  132. private:
  133. std::optional<Error> error_;
  134. };
  135. } // namespace smartbotic::common