pitrou commented on code in PR #36205:
URL: https://github.com/apache/arrow/pull/36205#discussion_r1238782508
##########
cpp/src/arrow/flight/types.cc:
##########
@@ -771,5 +774,258 @@ arrow::Result<std::string> BasicAuth::SerializeToString()
const {
Status BasicAuth::Serialize(const BasicAuth& basic_auth, std::string* out) {
return basic_auth.SerializeToString().Value(out);
}
+
+//------------------------------------------------------------
+// Error propagation helpers
+
+std::string ToString(TransportStatusCode code) {
+ switch (code) {
+ case TransportStatusCode::kOk:
+ return "kOk";
+ case TransportStatusCode::kUnknown:
+ return "kUnknown";
+ case TransportStatusCode::kInternal:
+ return "kInternal";
+ case TransportStatusCode::kInvalidArgument:
+ return "kInvalidArgument";
+ case TransportStatusCode::kTimedOut:
+ return "kTimedOut";
+ case TransportStatusCode::kNotFound:
+ return "kNotFound";
+ case TransportStatusCode::kAlreadyExists:
+ return "kAlreadyExists";
+ case TransportStatusCode::kCancelled:
+ return "kCancelled";
+ case TransportStatusCode::kUnauthenticated:
+ return "kUnauthenticated";
+ case TransportStatusCode::kUnauthorized:
+ return "kUnauthorized";
+ case TransportStatusCode::kUnimplemented:
+ return "kUnimplemented";
+ case TransportStatusCode::kUnavailable:
+ return "kUnavailable";
+ }
+ return "(unknown code)";
+}
+
+TransportStatus::Impl::Impl(TransportStatusCode code, std::string message,
+ std::vector<std::unique_ptr<StatusDetail>> details)
+ : code(code), message(std::move(message)), details(std::move(details)) {}
+TransportStatus::TransportStatus() = default;
+TransportStatus::TransportStatus(TransportStatusCode code)
+ : impl_(code == TransportStatusCode::kOk
+ ? nullptr
+ : std::make_unique<Impl>(code, "",
+
std::vector<std::unique_ptr<StatusDetail>>{})) {}
+TransportStatus::TransportStatus(TransportStatusCode code, std::string message)
+ : TransportStatus(code, std::move(message), {}) {}
+TransportStatus::TransportStatus(TransportStatusCode code, std::string message,
+ std::vector<std::unique_ptr<StatusDetail>>
details)
+ : impl_(std::make_unique<Impl>(code, std::move(message),
std::move(details))) {
+ if (code == TransportStatusCode::kOk) {
+ DCHECK(message.empty() && details.empty())
+ << "constructed kOk status with non-empty message/details";
+ }
+}
+
+std::string TransportStatus::ToString() const {
+ if (!impl_) {
+ return "TransportStatus{kOk}";
+ }
+ std::string str = "TransportStatus{";
+ str += arrow::flight::ToString(code());
+ str += ", message='";
+ str += message();
+ str += "', details={";
+ bool first = true;
+ for (const auto& detail : details()) {
+ if (!first) {
+ str += ", ";
+ }
+ first = false;
+
+ str += detail->ToString();
+ }
+ str += "}}";
+ return str;
+}
+
+TransportStatus TransportStatus::FromStatus(const Status& arrow_status) {
+ if (arrow_status.ok()) {
+ return TransportStatus{TransportStatusCode::kOk, ""};
+ }
+
+ TransportStatusCode code = TransportStatusCode::kUnknown;
+ std::string message = arrow_status.message();
+ if (arrow_status.detail()) {
+ message += ". Detail: ";
+ message += arrow_status.detail()->ToString();
+ }
+
+ std::shared_ptr<FlightStatusDetail> flight_status =
+ FlightStatusDetail::UnwrapStatus(arrow_status);
+ if (flight_status) {
+ switch (flight_status->code()) {
+ case FlightStatusCode::Internal:
+ code = TransportStatusCode::kInternal;
+ break;
+ case FlightStatusCode::TimedOut:
+ code = TransportStatusCode::kTimedOut;
+ break;
+ case FlightStatusCode::Cancelled:
+ code = TransportStatusCode::kCancelled;
+ break;
+ case FlightStatusCode::Unauthenticated:
+ code = TransportStatusCode::kUnauthenticated;
+ break;
+ case FlightStatusCode::Unauthorized:
+ code = TransportStatusCode::kUnauthorized;
+ break;
+ case FlightStatusCode::Unavailable:
+ code = TransportStatusCode::kUnavailable;
+ break;
+ default:
+ break;
+ }
+ } else if (arrow_status.IsKeyError()) {
+ code = TransportStatusCode::kNotFound;
+ } else if (arrow_status.IsInvalid()) {
+ code = TransportStatusCode::kInvalidArgument;
+ } else if (arrow_status.IsCancelled()) {
+ code = TransportStatusCode::kCancelled;
+ } else if (arrow_status.IsNotImplemented()) {
+ code = TransportStatusCode::kUnimplemented;
+ } else if (arrow_status.IsAlreadyExists()) {
+ code = TransportStatusCode::kAlreadyExists;
+ }
+ return TransportStatus{code, std::move(message)};
+}
+
+TransportStatus TransportStatus::FromCodeStringAndMessage(const std::string&
code_str,
+ std::string message)
{
+ int code_int = 0;
+ try {
+ code_int = std::stoi(code_str);
+ } catch (...) {
+ return TransportStatus{
+ TransportStatusCode::kUnknown,
+ message + ". Also, server sent unknown or invalid Arrow status code "
+ code_str};
Review Comment:
You mean "Flight status code"?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]