#include "common/error.hpp" #include namespace smartbotic::common { int errorCodeToHttpStatus(ErrorCode code) { switch (code) { case ErrorCode::InvalidArgument: case ErrorCode::ValidationError: case ErrorCode::QueryError: return 400; case ErrorCode::Unauthenticated: case ErrorCode::InvalidCredentials: case ErrorCode::TokenExpired: case ErrorCode::TokenInvalid: case ErrorCode::SessionExpired: return 401; case ErrorCode::PermissionDenied: case ErrorCode::InsufficientPermissions: return 403; case ErrorCode::NotFound: case ErrorCode::DocumentNotFound: case ErrorCode::CollectionNotFound: case ErrorCode::WorkflowNotFound: case ErrorCode::NodeNotFound: return 404; case ErrorCode::AlreadyExists: case ErrorCode::VersionConflict: return 409; case ErrorCode::FailedPrecondition: case ErrorCode::WorkflowInactive: return 412; case ErrorCode::ResourceExhausted: return 429; // Not 413: the request body may be small - a patch is refused when the // RESULT would be too big to read back. Nothing about the payload tells // the caller that, and retrying it unchanged never helps. case ErrorCode::DocumentTooLarge: return 422; // Deliberately not 408 or 503. Both invite the caller to repeat the // request, and the whole point of this code is that repeating a write // whose outcome is unknown is the one thing not to do. case ErrorCode::WriteOutcomeUnknown: return 504; case ErrorCode::Timeout: case ErrorCode::ExecutionTimeout: case ErrorCode::ScriptTimeout: return 408; case ErrorCode::Unavailable: case ErrorCode::RunnerUnavailable: case ErrorCode::NoRunnersAvailable: case ErrorCode::CircuitBreakerOpen: return 503; case ErrorCode::Aborted: case ErrorCode::ExecutionCancelled: return 499; default: return 500; } } Error::Error(ErrorCode code, std::string message) : code_(code), message_(std::move(message)), details_(nlohmann::json::object()) {} Error::Error(ErrorCode code, std::string message, nlohmann::json details) : code_(code), message_(std::move(message)), details_(std::move(details)) {} nlohmann::json Error::toJson() const { nlohmann::json j; j["code"] = static_cast(code_); j["message"] = message_; if (!details_.empty()) { j["details"] = details_; } return j; } std::string Error::toString() const { std::ostringstream ss; ss << "Error[" << static_cast(code_) << "]: " << message_; if (!details_.empty()) { ss << " (" << details_.dump() << ")"; } return ss.str(); } Error Error::fromException(const std::exception& e) { return Error(ErrorCode::Internal, e.what()); } SmartBoticException::SmartBoticException(Error error) : std::runtime_error(error.toString()), error_(std::move(error)) {} } // namespace smartbotic::common