Skip to content

Commit 268e108

Browse files
jihoonsonwesm
authored andcommitted
ARROW-251: Expose APIs for getting code and message of the status
Author: Jihoon Son <jihoonson@apache.org> Closes #114 from jihoonson/ARROW-251 and squashes the following commits: d1186bf [Jihoon Son] Fix compilation failure 4275c70 [Jihoon Son] Add tests for status 1162084 [Jihoon Son] Merge branch 'master' of https://github.com/apache/arrow into ARROW-251 a76b888 [Jihoon Son] Make code() public and add message()
1 parent 689cd27 commit 268e108

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

cpp/src/arrow/util/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ endif()
7070
ADD_ARROW_TEST(bit-util-test)
7171
ADD_ARROW_TEST(buffer-test)
7272
ADD_ARROW_TEST(memory-pool-test)
73+
ADD_ARROW_TEST(status-test)

cpp/src/arrow/util/status-test.cc

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include "gtest/gtest.h"
19+
20+
#include "arrow/util/status.h"
21+
#include "arrow/test-util.h"
22+
23+
namespace arrow {
24+
25+
TEST(StatusTest, TestCodeAndMessage) {
26+
Status ok = Status::OK();
27+
ASSERT_EQ(StatusCode::OK, ok.code());
28+
Status file_error = Status::IOError("file error");
29+
ASSERT_EQ(StatusCode::IOError, file_error.code());
30+
ASSERT_EQ("file error", file_error.message());
31+
}
32+
33+
TEST(StatusTest, TestToString) {
34+
Status file_error = Status::IOError("file error");
35+
ASSERT_EQ("IOError: file error", file_error.ToString());
36+
}
37+
38+
} // namespace arrow

cpp/src/arrow/util/status.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ class ARROW_EXPORT Status {
138138
// Get the POSIX code associated with this Status, or -1 if there is none.
139139
int16_t posix_code() const;
140140

141+
StatusCode code() const {
142+
return ((state_ == NULL) ? StatusCode::OK : static_cast<StatusCode>(state_[4]));
143+
}
144+
145+
std::string message() const {
146+
uint32_t length;
147+
memcpy(&length, state_, sizeof(length));
148+
std::string msg;
149+
msg.append((state_ + 7), length);
150+
return msg;
151+
}
152+
141153
private:
142154
// OK status has a NULL state_. Otherwise, state_ is a new[] array
143155
// of the following form:
@@ -147,10 +159,6 @@ class ARROW_EXPORT Status {
147159
// state_[7..] == message
148160
const char* state_;
149161

150-
StatusCode code() const {
151-
return ((state_ == NULL) ? StatusCode::OK : static_cast<StatusCode>(state_[4]));
152-
}
153-
154162
Status(StatusCode code, const std::string& msg, int16_t posix_code);
155163
static const char* CopyState(const char* s);
156164
};

0 commit comments

Comments
 (0)