]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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. | |
4 | // | |
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. | |
7 | // | |
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. | |
12 | ||
13 | #include "arrow/status.h" | |
14 | ||
15 | #include <cassert> | |
16 | #include <cstdlib> | |
17 | #include <iostream> | |
18 | #include <sstream> | |
19 | ||
20 | #include "arrow/util/logging.h" | |
21 | ||
22 | namespace arrow { | |
23 | ||
24 | Status::Status(StatusCode code, const std::string& msg) | |
25 | : Status::Status(code, msg, nullptr) {} | |
26 | ||
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"; | |
29 | state_ = new State; | |
30 | state_->code = code; | |
31 | state_->msg = std::move(msg); | |
32 | if (detail != nullptr) { | |
33 | state_->detail = std::move(detail); | |
34 | } | |
35 | } | |
36 | ||
37 | void Status::CopyFrom(const Status& s) { | |
38 | delete state_; | |
39 | if (s.state_ == nullptr) { | |
40 | state_ = nullptr; | |
41 | } else { | |
42 | state_ = new State(*s.state_); | |
43 | } | |
44 | } | |
45 | ||
46 | std::string Status::CodeAsString() const { | |
47 | if (state_ == nullptr) { | |
48 | return "OK"; | |
49 | } | |
50 | return CodeAsString(code()); | |
51 | } | |
52 | ||
53 | std::string Status::CodeAsString(StatusCode code) { | |
54 | const char* type; | |
55 | switch (code) { | |
56 | case StatusCode::OK: | |
57 | type = "OK"; | |
58 | break; | |
59 | case StatusCode::OutOfMemory: | |
60 | type = "Out of memory"; | |
61 | break; | |
62 | case StatusCode::KeyError: | |
63 | type = "Key error"; | |
64 | break; | |
65 | case StatusCode::TypeError: | |
66 | type = "Type error"; | |
67 | break; | |
68 | case StatusCode::Invalid: | |
69 | type = "Invalid"; | |
70 | break; | |
71 | case StatusCode::Cancelled: | |
72 | type = "Cancelled"; | |
73 | break; | |
74 | case StatusCode::IOError: | |
75 | type = "IOError"; | |
76 | break; | |
77 | case StatusCode::CapacityError: | |
78 | type = "Capacity error"; | |
79 | break; | |
80 | case StatusCode::IndexError: | |
81 | type = "Index error"; | |
82 | break; | |
83 | case StatusCode::UnknownError: | |
84 | type = "Unknown error"; | |
85 | break; | |
86 | case StatusCode::NotImplemented: | |
87 | type = "NotImplemented"; | |
88 | break; | |
89 | case StatusCode::SerializationError: | |
90 | type = "Serialization error"; | |
91 | break; | |
92 | case StatusCode::CodeGenError: | |
93 | type = "CodeGenError in Gandiva"; | |
94 | break; | |
95 | case StatusCode::ExpressionValidationError: | |
96 | type = "ExpressionValidationError"; | |
97 | break; | |
98 | case StatusCode::ExecutionError: | |
99 | type = "ExecutionError in Gandiva"; | |
100 | break; | |
101 | default: | |
102 | type = "Unknown"; | |
103 | break; | |
104 | } | |
105 | return std::string(type); | |
106 | } | |
107 | ||
108 | std::string Status::ToString() const { | |
109 | std::string result(CodeAsString()); | |
110 | if (state_ == nullptr) { | |
111 | return result; | |
112 | } | |
113 | result += ": "; | |
114 | result += state_->msg; | |
115 | if (state_->detail != nullptr) { | |
116 | result += ". Detail: "; | |
117 | result += state_->detail->ToString(); | |
118 | } | |
119 | ||
120 | return result; | |
121 | } | |
122 | ||
123 | void Status::Abort() const { Abort(std::string()); } | |
124 | ||
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"; | |
129 | } | |
130 | std::cerr << ToString() << std::endl; | |
131 | std::abort(); | |
132 | } | |
133 | ||
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(); | |
140 | } | |
141 | #endif | |
142 | ||
143 | } // namespace arrow |