error.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "common/error.hpp"
  2. #include <sstream>
  3. namespace smartbotic::common {
  4. int errorCodeToHttpStatus(ErrorCode code) {
  5. switch (code) {
  6. case ErrorCode::InvalidArgument:
  7. case ErrorCode::ValidationError:
  8. case ErrorCode::QueryError:
  9. return 400;
  10. case ErrorCode::Unauthenticated:
  11. case ErrorCode::InvalidCredentials:
  12. case ErrorCode::TokenExpired:
  13. case ErrorCode::TokenInvalid:
  14. case ErrorCode::SessionExpired:
  15. return 401;
  16. case ErrorCode::PermissionDenied:
  17. case ErrorCode::InsufficientPermissions:
  18. return 403;
  19. case ErrorCode::NotFound:
  20. case ErrorCode::DocumentNotFound:
  21. case ErrorCode::CollectionNotFound:
  22. case ErrorCode::WorkflowNotFound:
  23. case ErrorCode::NodeNotFound:
  24. return 404;
  25. case ErrorCode::AlreadyExists:
  26. case ErrorCode::VersionConflict:
  27. return 409;
  28. case ErrorCode::FailedPrecondition:
  29. case ErrorCode::WorkflowInactive:
  30. return 412;
  31. case ErrorCode::ResourceExhausted:
  32. return 429;
  33. // Not 413: the request body may be small - a patch is refused when the
  34. // RESULT would be too big to read back. Nothing about the payload tells
  35. // the caller that, and retrying it unchanged never helps.
  36. case ErrorCode::DocumentTooLarge:
  37. return 422;
  38. // Deliberately not 408 or 503. Both invite the caller to repeat the
  39. // request, and the whole point of this code is that repeating a write
  40. // whose outcome is unknown is the one thing not to do.
  41. case ErrorCode::WriteOutcomeUnknown:
  42. return 504;
  43. case ErrorCode::Timeout:
  44. case ErrorCode::ExecutionTimeout:
  45. case ErrorCode::ScriptTimeout:
  46. return 408;
  47. case ErrorCode::Unavailable:
  48. case ErrorCode::RunnerUnavailable:
  49. case ErrorCode::NoRunnersAvailable:
  50. case ErrorCode::CircuitBreakerOpen:
  51. return 503;
  52. case ErrorCode::Aborted:
  53. case ErrorCode::ExecutionCancelled:
  54. return 499;
  55. default:
  56. return 500;
  57. }
  58. }
  59. Error::Error(ErrorCode code, std::string message)
  60. : code_(code), message_(std::move(message)), details_(nlohmann::json::object()) {}
  61. Error::Error(ErrorCode code, std::string message, nlohmann::json details)
  62. : code_(code), message_(std::move(message)), details_(std::move(details)) {}
  63. nlohmann::json Error::toJson() const {
  64. nlohmann::json j;
  65. j["code"] = static_cast<int>(code_);
  66. j["message"] = message_;
  67. if (!details_.empty()) {
  68. j["details"] = details_;
  69. }
  70. return j;
  71. }
  72. std::string Error::toString() const {
  73. std::ostringstream ss;
  74. ss << "Error[" << static_cast<int>(code_) << "]: " << message_;
  75. if (!details_.empty()) {
  76. ss << " (" << details_.dump() << ")";
  77. }
  78. return ss.str();
  79. }
  80. Error Error::fromException(const std::exception& e) {
  81. return Error(ErrorCode::Internal, e.what());
  82. }
  83. SmartBoticException::SmartBoticException(Error error)
  84. : std::runtime_error(error.toString()), error_(std::move(error)) {}
  85. } // namespace smartbotic::common