Android-cuttlefish cvd tool
errors.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Portable error handling functions. This is only necessary for host-side
18// code that needs to be cross-platform; code that is only run on Unix should
19// just use errno and strerror() for simplicity.
20//
21// There is some complexity since Windows has (at least) three different error
22// numbers, not all of which share the same type:
23// * errno: for C runtime errors.
24// * GetLastError(): Windows non-socket errors.
25// * WSAGetLastError(): Windows socket errors.
26// errno can be passed to strerror() on all platforms, but the other two require
27// special handling to get the error string. Refer to Microsoft documentation
28// to determine which error code to check for each function.
29
30#pragma once
31
32#include <assert.h>
33
34#include <string>
35
36namespace android {
37namespace base {
38
39// Returns a string describing the given system error code. |error_code| must
40// be errno on Unix or GetLastError()/WSAGetLastError() on Windows. Passing
41// errno on Windows has undefined behavior.
42std::string SystemErrorCodeToString(int error_code);
43
44} // namespace base
45} // namespace android
46
47// Convenient macros for evaluating a statement, checking if the result is error, and returning it
48// to the caller.
49//
50// Usage with Result<T>:
51//
52// Result<Foo> getFoo() {...}
53//
54// Result<Bar> getBar() {
55// Foo foo = OR_RETURN(getFoo());
56// return Bar{foo};
57// }
58//
59// Usage with status_t:
60//
61// status_t getFoo(Foo*) {...}
62//
63// status_t getBar(Bar* bar) {
64// Foo foo;
65// OR_RETURN(getFoo(&foo));
66// *bar = Bar{foo};
67// return OK;
68// }
69//
70// Actually this can be used for any type as long as the OkOrFail<T> contract is satisfied. See
71// below.
72// If implicit conversion compilation errors occur involving a value type with a templated
73// forwarding ref ctor, compilation with cpp20 or explicitly converting to the desired
74// return type is required.
75#define OR_RETURN(expr) \
76 ({ \
77 decltype(expr)&& __or_return_expr = (expr); \
78 typedef android::base::OkOrFail<std::remove_reference_t<decltype(__or_return_expr)>> \
79 ok_or_fail; \
80 if (!ok_or_fail::IsOk(__or_return_expr)) { \
81 return ok_or_fail::Fail(std::move(__or_return_expr)); \
82 } \
83 ok_or_fail::Unwrap(std::move(__or_return_expr)); \
84 })
85
86// Same as OR_RETURN, but aborts if expr is a failure.
87#if defined(__BIONIC__)
88#define OR_FATAL(expr) \
89 ({ \
90 decltype(expr)&& __or_fatal_expr = (expr); \
91 typedef android::base::OkOrFail<std::remove_reference_t<decltype(__or_fatal_expr)>> \
92 ok_or_fail; \
93 if (!ok_or_fail::IsOk(__or_fatal_expr)) { \
94 __assert(__FILE__, __LINE__, ok_or_fail::ErrorMessage(__or_fatal_expr).c_str()); \
95 } \
96 ok_or_fail::Unwrap(std::move(__or_fatal_expr)); \
97 })
98#else
99#define OR_FATAL(expr) \
100 ({ \
101 decltype(expr)&& __or_fatal_expr = (expr); \
102 typedef android::base::OkOrFail<std::remove_reference_t<decltype(__or_fatal_expr)>> \
103 ok_or_fail; \
104 if (!ok_or_fail::IsOk(__or_fatal_expr)) { \
105 fprintf(stderr, "%s:%d: assertion \"%s\" failed", __FILE__, __LINE__, \
106 ok_or_fail::ErrorMessage(__or_fatal_expr).c_str()); \
107 abort(); \
108 } \
109 ok_or_fail::Unwrap(std::move(__or_fatal_expr)); \
110 })
111#endif
112
113namespace android {
114namespace base {
115
116// The OkOrFail contract for a type T. This must be implemented for a type T if you want to use
117// OR_RETURN(stmt) where stmt evalues to a value of type T.
118template <typename T, typename = void>
119struct OkOrFail {
120 // Checks if T is ok or fail.
121 static bool IsOk(const T&);
122
123 // Turns T into the success value.
124 template <typename U>
125 static U Unwrap(T&&);
126
127 // Moves T into OkOrFail<T>, so that we can convert it to other types
128 OkOrFail(T&& v);
129 OkOrFail() = delete;
130 OkOrFail(const T&) = delete;
131
132 // And there need to be one or more conversion operators that turns the error value of T into a
133 // target type. For example, for T = Result<V, E>, there can be ...
134 //
135 // // for the case where OR_RETURN is called in a function expecting E
136 // operator E()&& { return val_.error().code(); }
137 //
138 // // for the case where OR_RETURN is called in a function expecting Result<U, E>
139 // template <typename U>
140 // operator Result<U, E>()&& { return val_.error(); }
141
142 // Returns the string representation of the fail value.
143 static std::string ErrorMessage(const T& v);
144};
145
146} // namespace base
147} // namespace android
std::string SystemErrorCodeToString(int error_code)
Definition: errors_unix.cpp:25
Definition: map_ptr.h:34
Definition: errors.h:119
OkOrFail(const T &)=delete
static std::string ErrorMessage(const T &v)
static U Unwrap(T &&)
static bool IsOk(const T &)