error.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // Auth errors (3xxx)
  31. AuthError = 3000,
  32. InvalidCredentials = 3001,
  33. TokenExpired = 3002,
  34. TokenInvalid = 3003,
  35. SessionExpired = 3004,
  36. InsufficientPermissions = 3005,
  37. // Workflow errors (4xxx)
  38. WorkflowError = 4000,
  39. WorkflowNotFound = 4001,
  40. WorkflowInactive = 4002,
  41. NodeNotFound = 4003,
  42. NodeExecutionFailed = 4004,
  43. ExecutionTimeout = 4005,
  44. ExecutionCancelled = 4006,
  45. CircuitBreakerOpen = 4007,
  46. // Runner errors (5xxx)
  47. RunnerError = 5000,
  48. RunnerUnavailable = 5001,
  49. NoRunnersAvailable = 5002,
  50. ScriptError = 5003,
  51. ScriptTimeout = 5004,
  52. };
  53. // Convert error code to HTTP status
  54. int errorCodeToHttpStatus(ErrorCode code);
  55. // Error class with detailed information
  56. class Error {
  57. public:
  58. Error(ErrorCode code, std::string message);
  59. Error(ErrorCode code, std::string message, nlohmann::json details);
  60. ErrorCode code() const { return code_; }
  61. const std::string& message() const { return message_; }
  62. const nlohmann::json& details() const { return details_; }
  63. nlohmann::json toJson() const;
  64. std::string toString() const;
  65. static Error fromException(const std::exception& e);
  66. private:
  67. ErrorCode code_;
  68. std::string message_;
  69. nlohmann::json details_;
  70. };
  71. // Exception class that wraps Error
  72. class SmartBoticException : public std::runtime_error {
  73. public:
  74. explicit SmartBoticException(Error error);
  75. const Error& error() const { return error_; }
  76. ErrorCode code() const { return error_.code(); }
  77. private:
  78. Error error_;
  79. };
  80. // Result type for operations that can fail
  81. template<typename T>
  82. class Result {
  83. public:
  84. Result(T value) : data_(std::move(value)) {}
  85. Result(Error error) : data_(std::move(error)) {}
  86. bool ok() const { return std::holds_alternative<T>(data_); }
  87. bool failed() const { return !ok(); }
  88. const T& value() const { return std::get<T>(data_); }
  89. T& value() { return std::get<T>(data_); }
  90. const Error& error() const { return std::get<Error>(data_); }
  91. // Throw if error
  92. const T& valueOrThrow() const {
  93. if (failed()) {
  94. throw SmartBoticException(error());
  95. }
  96. return value();
  97. }
  98. T& valueOrThrow() {
  99. if (failed()) {
  100. throw SmartBoticException(error());
  101. }
  102. return value();
  103. }
  104. private:
  105. std::variant<T, Error> data_;
  106. };
  107. // Void result specialization
  108. template<>
  109. class Result<void> {
  110. public:
  111. Result() : error_(std::nullopt) {}
  112. Result(Error error) : error_(std::move(error)) {}
  113. bool ok() const { return !error_.has_value(); }
  114. bool failed() const { return error_.has_value(); }
  115. const Error& error() const { return *error_; }
  116. void throwIfFailed() const {
  117. if (failed()) {
  118. throw SmartBoticException(*error_);
  119. }
  120. }
  121. private:
  122. std::optional<Error> error_;
  123. };
  124. } // namespace smartbotic::common