2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <system_error>
25 #include <folly/Conv.h>
26 #include <folly/FBString.h>
27 #include <folly/Likely.h>
28 #include <folly/Portability.h>
32 // Various helpers to throw appropriate std::system_error exceptions from C
33 // library errors (returned in errno, as positive return values (many POSIX
34 // functions), or as negative return values (Linux syscalls))
36 // The *Explicit functions take an explicit value for errno.
38 // Helper to throw std::system_error
39 [[noreturn]] inline void throwSystemErrorExplicit(int err, const char* msg) {
40 throw std::system_error(err, std::system_category(), msg);
43 template <class... Args>
44 [[noreturn]] void throwSystemErrorExplicit(int err, Args&&... args) {
45 throwSystemErrorExplicit(
46 err, to<fbstring>(std::forward<Args>(args)...).c_str());
49 // Helper to throw std::system_error from errno and components of a string
50 template <class... Args>
51 [[noreturn]] void throwSystemError(Args&&... args) {
52 throwSystemErrorExplicit(errno, std::forward<Args>(args)...);
55 // Check a Posix return code (0 on success, error number on error), throw
57 template <class... Args>
58 void checkPosixError(int err, Args&&... args) {
59 if (UNLIKELY(err != 0)) {
60 throwSystemErrorExplicit(err, std::forward<Args>(args)...);
64 // Check a Linux kernel-style return code (>= 0 on success, negative error
65 // number on error), throw on error.
66 template <class... Args>
67 void checkKernelError(ssize_t ret, Args&&... args) {
68 if (UNLIKELY(ret < 0)) {
69 throwSystemErrorExplicit(int(-ret), std::forward<Args>(args)...);
73 // Check a traditional Unix return code (-1 and sets errno on error), throw
75 template <class... Args>
76 void checkUnixError(ssize_t ret, Args&&... args) {
77 if (UNLIKELY(ret == -1)) {
78 throwSystemError(std::forward<Args>(args)...);
82 template <class... Args>
83 void checkUnixErrorExplicit(ssize_t ret, int savedErrno, Args&&... args) {
84 if (UNLIKELY(ret == -1)) {
85 throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
89 // Check the return code from a fopen-style function (returns a non-nullptr
90 // FILE* on success, nullptr on error, sets errno). Works with fopen, fdopen,
91 // freopen, tmpfile, etc.
92 template <class... Args>
93 void checkFopenError(FILE* fp, Args&&... args) {
95 throwSystemError(std::forward<Args>(args)...);
99 template <class... Args>
100 void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) {
102 throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
106 template <typename E, typename V, typename... Args>
107 void throwOnFail(V&& value, Args&&... args) {
109 throw E(std::forward<Args>(args)...);
114 * If cond is not true, raise an exception of type E. E must have a ctor that
115 * works with const char* (a description of the failure).
117 #define CHECK_THROW(cond, E) \
118 ::folly::throwOnFail<E>((cond), "Check failed: " #cond)