| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- #include "common/error.hpp"
- #include <sstream>
- 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<int>(code_);
- j["message"] = message_;
- if (!details_.empty()) {
- j["details"] = details_;
- }
- return j;
- }
- std::string Error::toString() const {
- std::ostringstream ss;
- ss << "Error[" << static_cast<int>(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
|