1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
5 // A Status encapsulates the result of an operation. It may indicate success,
6 // or it may indicate an error with an associated error message.
8 // Multiple threads can invoke const methods on a Status without
9 // external synchronization, but if any of the threads may call a
10 // non-const method, all threads accessing the same Status must use
11 // external synchronization.
13 #include "arrow/status.h"
20 #include "arrow/util/logging.h"
24 Status::Status(StatusCode code
, const std::string
& msg
)
25 : Status::Status(code
, msg
, nullptr) {}
27 Status::Status(StatusCode code
, std::string msg
, std::shared_ptr
<StatusDetail
> detail
) {
28 ARROW_CHECK_NE(code
, StatusCode::OK
) << "Cannot construct ok status with message";
31 state_
->msg
= std::move(msg
);
32 if (detail
!= nullptr) {
33 state_
->detail
= std::move(detail
);
37 void Status::CopyFrom(const Status
& s
) {
39 if (s
.state_
== nullptr) {
42 state_
= new State(*s
.state_
);
46 std::string
Status::CodeAsString() const {
47 if (state_
== nullptr) {
50 return CodeAsString(code());
53 std::string
Status::CodeAsString(StatusCode code
) {
59 case StatusCode::OutOfMemory
:
60 type
= "Out of memory";
62 case StatusCode::KeyError
:
65 case StatusCode::TypeError
:
68 case StatusCode::Invalid
:
71 case StatusCode::Cancelled
:
74 case StatusCode::IOError
:
77 case StatusCode::CapacityError
:
78 type
= "Capacity error";
80 case StatusCode::IndexError
:
83 case StatusCode::UnknownError
:
84 type
= "Unknown error";
86 case StatusCode::NotImplemented
:
87 type
= "NotImplemented";
89 case StatusCode::SerializationError
:
90 type
= "Serialization error";
92 case StatusCode::CodeGenError
:
93 type
= "CodeGenError in Gandiva";
95 case StatusCode::ExpressionValidationError
:
96 type
= "ExpressionValidationError";
98 case StatusCode::ExecutionError
:
99 type
= "ExecutionError in Gandiva";
105 return std::string(type
);
108 std::string
Status::ToString() const {
109 std::string
result(CodeAsString());
110 if (state_
== nullptr) {
114 result
+= state_
->msg
;
115 if (state_
->detail
!= nullptr) {
116 result
+= ". Detail: ";
117 result
+= state_
->detail
->ToString();
123 void Status::Abort() const { Abort(std::string()); }
125 void Status::Abort(const std::string
& message
) const {
126 std::cerr
<< "-- Arrow Fatal Error --\n";
127 if (!message
.empty()) {
128 std::cerr
<< message
<< "\n";
130 std::cerr
<< ToString() << std::endl
;
134 #ifdef ARROW_EXTRA_ERROR_CONTEXT
135 void Status::AddContextLine(const char* filename
, int line
, const char* expr
) {
136 ARROW_CHECK(!ok()) << "Cannot add context line to ok status";
137 std::stringstream ss
;
138 ss
<< "\n" << filename
<< ":" << line
<< " " << expr
;
139 state_
->msg
+= ss
.str();