Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-251: Expose APIs for getting code and message of the status #114

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/src/arrow/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ endif()
ADD_ARROW_TEST(bit-util-test)
ADD_ARROW_TEST(buffer-test)
ADD_ARROW_TEST(memory-pool-test)
ADD_ARROW_TEST(status-test)
38 changes: 38 additions & 0 deletions cpp/src/arrow/util/status-test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "gtest/gtest.h"

#include "arrow/util/status.h"
#include "arrow/test-util.h"

namespace arrow {

TEST(StatusTest, TestCodeAndMessage) {
Status ok = Status::OK();
ASSERT_EQ(StatusCode::OK, ok.code());
Status file_error = Status::IOError("file error");
ASSERT_EQ(StatusCode::IOError, file_error.code());
ASSERT_EQ("file error", file_error.message());
}

TEST(StatusTest, TestToString) {
Status file_error = Status::IOError("file error");
ASSERT_EQ("IOError: file error", file_error.ToString());
}

} // namespace arrow
16 changes: 12 additions & 4 deletions cpp/src/arrow/util/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ class ARROW_EXPORT Status {
// Get the POSIX code associated with this Status, or -1 if there is none.
int16_t posix_code() const;

StatusCode code() const {
return ((state_ == NULL) ? StatusCode::OK : static_cast<StatusCode>(state_[4]));
}

std::string message() const {
uint32_t length;
memcpy(&length, state_, sizeof(length));
std::string msg;
msg.append((state_ + 7), length);
return msg;
}

private:
// OK status has a NULL state_. Otherwise, state_ is a new[] array
// of the following form:
Expand All @@ -147,10 +159,6 @@ class ARROW_EXPORT Status {
// state_[7..] == message
const char* state_;

StatusCode code() const {
return ((state_ == NULL) ? StatusCode::OK : static_cast<StatusCode>(state_[4]));
}

Status(StatusCode code, const std::string& msg, int16_t posix_code);
static const char* CopyState(const char* s);
};
Expand Down