xref: /aosp_15_r20/external/abseil-cpp/absl/status/statusor.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2020 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker #include "absl/status/statusor.h"
15*9356374aSAndroid Build Coastguard Worker 
16*9356374aSAndroid Build Coastguard Worker #include <cstdlib>
17*9356374aSAndroid Build Coastguard Worker #include <utility>
18*9356374aSAndroid Build Coastguard Worker 
19*9356374aSAndroid Build Coastguard Worker #include "absl/base/call_once.h"
20*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
21*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/raw_logging.h"
22*9356374aSAndroid Build Coastguard Worker #include "absl/base/nullability.h"
23*9356374aSAndroid Build Coastguard Worker #include "absl/status/internal/statusor_internal.h"
24*9356374aSAndroid Build Coastguard Worker #include "absl/status/status.h"
25*9356374aSAndroid Build Coastguard Worker #include "absl/strings/str_cat.h"
26*9356374aSAndroid Build Coastguard Worker 
27*9356374aSAndroid Build Coastguard Worker namespace absl {
28*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
29*9356374aSAndroid Build Coastguard Worker 
BadStatusOrAccess(absl::Status status)30*9356374aSAndroid Build Coastguard Worker BadStatusOrAccess::BadStatusOrAccess(absl::Status status)
31*9356374aSAndroid Build Coastguard Worker     : status_(std::move(status)) {}
32*9356374aSAndroid Build Coastguard Worker 
BadStatusOrAccess(const BadStatusOrAccess & other)33*9356374aSAndroid Build Coastguard Worker BadStatusOrAccess::BadStatusOrAccess(const BadStatusOrAccess& other)
34*9356374aSAndroid Build Coastguard Worker     : status_(other.status_) {}
35*9356374aSAndroid Build Coastguard Worker 
operator =(const BadStatusOrAccess & other)36*9356374aSAndroid Build Coastguard Worker BadStatusOrAccess& BadStatusOrAccess::operator=(
37*9356374aSAndroid Build Coastguard Worker     const BadStatusOrAccess& other) {
38*9356374aSAndroid Build Coastguard Worker   // Ensure assignment is correct regardless of whether this->InitWhat() has
39*9356374aSAndroid Build Coastguard Worker   // already been called.
40*9356374aSAndroid Build Coastguard Worker   other.InitWhat();
41*9356374aSAndroid Build Coastguard Worker   status_ = other.status_;
42*9356374aSAndroid Build Coastguard Worker   what_ = other.what_;
43*9356374aSAndroid Build Coastguard Worker   return *this;
44*9356374aSAndroid Build Coastguard Worker }
45*9356374aSAndroid Build Coastguard Worker 
operator =(BadStatusOrAccess && other)46*9356374aSAndroid Build Coastguard Worker BadStatusOrAccess& BadStatusOrAccess::operator=(BadStatusOrAccess&& other) {
47*9356374aSAndroid Build Coastguard Worker   // Ensure assignment is correct regardless of whether this->InitWhat() has
48*9356374aSAndroid Build Coastguard Worker   // already been called.
49*9356374aSAndroid Build Coastguard Worker   other.InitWhat();
50*9356374aSAndroid Build Coastguard Worker   status_ = std::move(other.status_);
51*9356374aSAndroid Build Coastguard Worker   what_ = std::move(other.what_);
52*9356374aSAndroid Build Coastguard Worker   return *this;
53*9356374aSAndroid Build Coastguard Worker }
54*9356374aSAndroid Build Coastguard Worker 
BadStatusOrAccess(BadStatusOrAccess && other)55*9356374aSAndroid Build Coastguard Worker BadStatusOrAccess::BadStatusOrAccess(BadStatusOrAccess&& other)
56*9356374aSAndroid Build Coastguard Worker     : status_(std::move(other.status_)) {}
57*9356374aSAndroid Build Coastguard Worker 
what() const58*9356374aSAndroid Build Coastguard Worker absl::Nonnull<const char*> BadStatusOrAccess::what() const noexcept {
59*9356374aSAndroid Build Coastguard Worker   InitWhat();
60*9356374aSAndroid Build Coastguard Worker   return what_.c_str();
61*9356374aSAndroid Build Coastguard Worker }
62*9356374aSAndroid Build Coastguard Worker 
status() const63*9356374aSAndroid Build Coastguard Worker const absl::Status& BadStatusOrAccess::status() const { return status_; }
64*9356374aSAndroid Build Coastguard Worker 
InitWhat() const65*9356374aSAndroid Build Coastguard Worker void BadStatusOrAccess::InitWhat() const {
66*9356374aSAndroid Build Coastguard Worker   absl::call_once(init_what_, [this] {
67*9356374aSAndroid Build Coastguard Worker     what_ = absl::StrCat("Bad StatusOr access: ", status_.ToString());
68*9356374aSAndroid Build Coastguard Worker   });
69*9356374aSAndroid Build Coastguard Worker }
70*9356374aSAndroid Build Coastguard Worker 
71*9356374aSAndroid Build Coastguard Worker namespace internal_statusor {
72*9356374aSAndroid Build Coastguard Worker 
HandleInvalidStatusCtorArg(absl::Nonnull<absl::Status * > status)73*9356374aSAndroid Build Coastguard Worker void Helper::HandleInvalidStatusCtorArg(absl::Nonnull<absl::Status*> status) {
74*9356374aSAndroid Build Coastguard Worker   const char* kMessage =
75*9356374aSAndroid Build Coastguard Worker       "An OK status is not a valid constructor argument to StatusOr<T>";
76*9356374aSAndroid Build Coastguard Worker #ifdef NDEBUG
77*9356374aSAndroid Build Coastguard Worker   ABSL_INTERNAL_LOG(ERROR, kMessage);
78*9356374aSAndroid Build Coastguard Worker #else
79*9356374aSAndroid Build Coastguard Worker   ABSL_INTERNAL_LOG(FATAL, kMessage);
80*9356374aSAndroid Build Coastguard Worker #endif
81*9356374aSAndroid Build Coastguard Worker   // In optimized builds, we will fall back to InternalError.
82*9356374aSAndroid Build Coastguard Worker   *status = absl::InternalError(kMessage);
83*9356374aSAndroid Build Coastguard Worker }
84*9356374aSAndroid Build Coastguard Worker 
Crash(const absl::Status & status)85*9356374aSAndroid Build Coastguard Worker void Helper::Crash(const absl::Status& status) {
86*9356374aSAndroid Build Coastguard Worker   ABSL_INTERNAL_LOG(
87*9356374aSAndroid Build Coastguard Worker       FATAL,
88*9356374aSAndroid Build Coastguard Worker       absl::StrCat("Attempting to fetch value instead of handling error ",
89*9356374aSAndroid Build Coastguard Worker                    status.ToString()));
90*9356374aSAndroid Build Coastguard Worker }
91*9356374aSAndroid Build Coastguard Worker 
ThrowBadStatusOrAccess(absl::Status status)92*9356374aSAndroid Build Coastguard Worker void ThrowBadStatusOrAccess(absl::Status status) {
93*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
94*9356374aSAndroid Build Coastguard Worker   throw absl::BadStatusOrAccess(std::move(status));
95*9356374aSAndroid Build Coastguard Worker #else
96*9356374aSAndroid Build Coastguard Worker   ABSL_INTERNAL_LOG(
97*9356374aSAndroid Build Coastguard Worker       FATAL,
98*9356374aSAndroid Build Coastguard Worker       absl::StrCat("Attempting to fetch value instead of handling error ",
99*9356374aSAndroid Build Coastguard Worker                    status.ToString()));
100*9356374aSAndroid Build Coastguard Worker   std::abort();
101*9356374aSAndroid Build Coastguard Worker #endif
102*9356374aSAndroid Build Coastguard Worker }
103*9356374aSAndroid Build Coastguard Worker 
104*9356374aSAndroid Build Coastguard Worker }  // namespace internal_statusor
105*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
106*9356374aSAndroid Build Coastguard Worker }  // namespace absl
107