summaryrefslogtreecommitdiff
path: root/absl/status
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2022-04-19 13:56:51 -0700
committerGravatar Andy Getz <durandal@google.com>2022-04-19 18:08:40 -0400
commit3dccef2a91243460364312d3e1100ff1d573fb1d (patch)
tree21dc4da934d938d21c013caae13a2be8c8a6ca29 /absl/status
parentc27ab06897f330267bed99061ed3e523e2606bf1 (diff)
Export of internal Abseil changes
-- 6457ad659de86ce4cae1e9f7cb03a701c6c2851e by Abseil Team <absl-team@google.com>: Introduced ErrnoToStatusCode and ErrnoToStatus to abseil. PiperOrigin-RevId: 442903450 Change-Id: I9c062b34a3811216f43eef56e631eada3b4e3e84 GitOrigin-RevId: 6457ad659de86ce4cae1e9f7cb03a701c6c2851e
Diffstat (limited to 'absl/status')
-rw-r--r--absl/status/BUILD.bazel2
-rw-r--r--absl/status/CMakeLists.txt11
-rw-r--r--absl/status/status.cc156
-rw-r--r--absl/status/status.h13
-rw-r--r--absl/status/status_test.cc20
5 files changed, 196 insertions, 6 deletions
diff --git a/absl/status/BUILD.bazel b/absl/status/BUILD.bazel
index bae5156f..b334f31d 100644
--- a/absl/status/BUILD.bazel
+++ b/absl/status/BUILD.bazel
@@ -41,9 +41,9 @@ cc_library(
copts = ABSL_DEFAULT_COPTS,
deps = [
"//absl/base:atomic_hook",
- "//absl/base:config",
"//absl/base:core_headers",
"//absl/base:raw_logging_internal",
+ "//absl/base:strerror",
"//absl/container:inlined_vector",
"//absl/debugging:stacktrace",
"//absl/debugging:symbolize",
diff --git a/absl/status/CMakeLists.txt b/absl/status/CMakeLists.txt
index f107c85b..15db36af 100644
--- a/absl/status/CMakeLists.txt
+++ b/absl/status/CMakeLists.txt
@@ -28,16 +28,17 @@ absl_cc_library(
DEPS
absl::atomic_hook
absl::config
+ absl::cord
absl::core_headers
absl::function_ref
- absl::raw_logging_internal
absl::inlined_vector
+ absl::optional
+ absl::raw_logging_internal
absl::stacktrace
- absl::symbolize
- absl::strings
- absl::cord
absl::str_format
- absl::optional
+ absl::strerror
+ absl::strings
+ absl::symbolize
PUBLIC
)
diff --git a/absl/status/status.cc b/absl/status/status.cc
index 6b316ac6..fc5a1425 100644
--- a/absl/status/status.cc
+++ b/absl/status/status.cc
@@ -13,9 +13,12 @@
// limitations under the License.
#include "absl/status/status.h"
+#include <errno.h>
+
#include <cassert>
#include "absl/base/internal/raw_logging.h"
+#include "absl/base/internal/strerror.h"
#include "absl/debugging/stacktrace.h"
#include "absl/debugging/symbolize.h"
#include "absl/status/status_payload_printer.h"
@@ -443,5 +446,158 @@ bool IsUnknown(const Status& status) {
return status.code() == absl::StatusCode::kUnknown;
}
+StatusCode ErrnoToStatusCode(int error_number) {
+ switch (error_number) {
+ case 0:
+ return StatusCode::kOk;
+ case EINVAL: // Invalid argument
+ case ENAMETOOLONG: // Filename too long
+ case E2BIG: // Argument list too long
+ case EDESTADDRREQ: // Destination address required
+ case EDOM: // Mathematics argument out of domain of function
+ case EFAULT: // Bad address
+ case EILSEQ: // Illegal byte sequence
+ case ENOPROTOOPT: // Protocol not available
+ case ENOSTR: // Not a STREAM
+ case ENOTSOCK: // Not a socket
+ case ENOTTY: // Inappropriate I/O control operation
+ case EPROTOTYPE: // Protocol wrong type for socket
+ case ESPIPE: // Invalid seek
+ return StatusCode::kInvalidArgument;
+ case ETIMEDOUT: // Connection timed out
+ case ETIME: // Timer expired
+ return StatusCode::kDeadlineExceeded;
+ case ENODEV: // No such device
+ case ENOENT: // No such file or directory
+#ifdef ENOMEDIUM
+ case ENOMEDIUM: // No medium found
+#endif
+ case ENXIO: // No such device or address
+ case ESRCH: // No such process
+ return StatusCode::kNotFound;
+ case EEXIST: // File exists
+ case EADDRNOTAVAIL: // Address not available
+ case EALREADY: // Connection already in progress
+#ifdef ENOTUNIQ
+ case ENOTUNIQ: // Name not unique on network
+#endif
+ return StatusCode::kAlreadyExists;
+ case EPERM: // Operation not permitted
+ case EACCES: // Permission denied
+#ifdef ENOKEY
+ case ENOKEY: // Required key not available
+#endif
+ case EROFS: // Read only file system
+ return StatusCode::kPermissionDenied;
+ case ENOTEMPTY: // Directory not empty
+ case EISDIR: // Is a directory
+ case ENOTDIR: // Not a directory
+ case EADDRINUSE: // Address already in use
+ case EBADF: // Invalid file descriptor
+#ifdef EBADFD
+ case EBADFD: // File descriptor in bad state
+#endif
+ case EBUSY: // Device or resource busy
+ case ECHILD: // No child processes
+ case EISCONN: // Socket is connected
+#ifdef EISNAM
+ case EISNAM: // Is a named type file
+#endif
+#ifdef ENOTBLK
+ case ENOTBLK: // Block device required
+#endif
+ case ENOTCONN: // The socket is not connected
+ case EPIPE: // Broken pipe
+#ifdef ESHUTDOWN
+ case ESHUTDOWN: // Cannot send after transport endpoint shutdown
+#endif
+ case ETXTBSY: // Text file busy
+#ifdef EUNATCH
+ case EUNATCH: // Protocol driver not attached
+#endif
+ return StatusCode::kFailedPrecondition;
+ case ENOSPC: // No space left on device
+#ifdef EDQUOT
+ case EDQUOT: // Disk quota exceeded
+#endif
+ case EMFILE: // Too many open files
+ case EMLINK: // Too many links
+ case ENFILE: // Too many open files in system
+ case ENOBUFS: // No buffer space available
+ case ENODATA: // No message is available on the STREAM read queue
+ case ENOMEM: // Not enough space
+ case ENOSR: // No STREAM resources
+#ifdef EUSERS
+ case EUSERS: // Too many users
+#endif
+ return StatusCode::kResourceExhausted;
+#ifdef ECHRNG
+ case ECHRNG: // Channel number out of range
+#endif
+ case EFBIG: // File too large
+ case EOVERFLOW: // Value too large to be stored in data type
+ case ERANGE: // Result too large
+ return StatusCode::kOutOfRange;
+#ifdef ENOPKG
+ case ENOPKG: // Package not installed
+#endif
+ case ENOSYS: // Function not implemented
+ case ENOTSUP: // Operation not supported
+ case EAFNOSUPPORT: // Address family not supported
+#ifdef EPFNOSUPPORT
+ case EPFNOSUPPORT: // Protocol family not supported
+#endif
+ case EPROTONOSUPPORT: // Protocol not supported
+#ifdef ESOCKTNOSUPPORT
+ case ESOCKTNOSUPPORT: // Socket type not supported
+#endif
+ case EXDEV: // Improper link
+ return StatusCode::kUnimplemented;
+ case EAGAIN: // Resource temporarily unavailable
+#ifdef ECOMM
+ case ECOMM: // Communication error on send
+#endif
+ case ECONNREFUSED: // Connection refused
+ case ECONNABORTED: // Connection aborted
+ case ECONNRESET: // Connection reset
+ case EINTR: // Interrupted function call
+#ifdef EHOSTDOWN
+ case EHOSTDOWN: // Host is down
+#endif
+ case EHOSTUNREACH: // Host is unreachable
+ case ENETDOWN: // Network is down
+ case ENETRESET: // Connection aborted by network
+ case ENETUNREACH: // Network unreachable
+ case ENOLCK: // No locks available
+ case ENOLINK: // Link has been severed
+#ifdef ENONET
+ case ENONET: // Machine is not on the network
+#endif
+ return StatusCode::kUnavailable;
+ case EDEADLK: // Resource deadlock avoided
+#ifdef ESTALE
+ case ESTALE: // Stale file handle
+#endif
+ return StatusCode::kAborted;
+ case ECANCELED: // Operation cancelled
+ return StatusCode::kCancelled;
+ default:
+ return StatusCode::kUnknown;
+ }
+}
+
+namespace {
+std::string MessageForErrnoToStatus(int error_number,
+ absl::string_view message) {
+ return absl::StrCat(message, ": ",
+ absl::base_internal::StrError(error_number));
+}
+} // namespace
+
+Status ErrnoToStatus(int error_number, absl::string_view message) {
+ return Status(ErrnoToStatusCode(error_number),
+ MessageForErrnoToStatus(error_number, message));
+}
+
ABSL_NAMESPACE_END
} // namespace absl
diff --git a/absl/status/status.h b/absl/status/status.h
index 193db8d8..9292b83a 100644
--- a/absl/status/status.h
+++ b/absl/status/status.h
@@ -738,6 +738,19 @@ Status UnavailableError(absl::string_view message);
Status UnimplementedError(absl::string_view message);
Status UnknownError(absl::string_view message);
+// ErrnoToStatusCode()
+//
+// Returns the StatusCode for `error_number`, which should be an `errno` value.
+// See https://en.cppreference.com/w/cpp/error/errno_macros and similar
+// references.
+absl::StatusCode ErrnoToStatusCode(int error_number);
+
+// ErrnoToStatus()
+//
+// Convenience function that creates a `absl::Status` using an `error_number`,
+// which should be an `errno` value.
+Status ErrnoToStatus(int error_number, absl::string_view message);
+
//------------------------------------------------------------------------------
// Implementation details follow
//------------------------------------------------------------------------------
diff --git a/absl/status/status_test.cc b/absl/status/status_test.cc
index 1b038f6d..89cce7df 100644
--- a/absl/status/status_test.cc
+++ b/absl/status/status_test.cc
@@ -14,6 +14,8 @@
#include "absl/status/status.h"
+#include <errno.h>
+
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/strings/str_cat.h"
@@ -485,4 +487,22 @@ TEST(Status, Swap) {
test_swap(no_payload, with_payload);
test_swap(with_payload, no_payload);
}
+
+TEST(StatusErrno, ErrnoToStatusCode) {
+ EXPECT_EQ(absl::ErrnoToStatusCode(0), absl::StatusCode::kOk);
+
+ // Spot-check a few errno values.
+ EXPECT_EQ(absl::ErrnoToStatusCode(EINVAL),
+ absl::StatusCode::kInvalidArgument);
+ EXPECT_EQ(absl::ErrnoToStatusCode(ENOENT), absl::StatusCode::kNotFound);
+
+ // We'll pick a very large number so it hopefully doesn't collide to errno.
+ EXPECT_EQ(absl::ErrnoToStatusCode(19980927), absl::StatusCode::kUnknown);
+}
+
+TEST(StatusErrno, ErrnoToStatus) {
+ absl::Status status = absl::ErrnoToStatus(ENOENT, "Cannot open 'path'");
+ EXPECT_EQ(status.code(), absl::StatusCode::kNotFound);
+ EXPECT_EQ(status.message(), "Cannot open 'path': No such file or directory");
+}
} // namespace