Fix the last issues with exception_wrapper under MSVC
[folly.git] / folly / Exception.h
1 /*
2  * Copyright 2017 Facebook, Inc.
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 #pragma once
18
19 #include <errno.h>
20
21 #include <cstdio>
22 #include <stdexcept>
23 #include <system_error>
24
25 #include <folly/Conv.h>
26 #include <folly/FBString.h>
27 #include <folly/Likely.h>
28 #include <folly/Portability.h>
29
30 namespace folly {
31
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))
35 //
36 // The *Explicit functions take an explicit value for errno.
37
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);
41 }
42
43 template <class... Args>
44 [[noreturn]] void throwSystemErrorExplicit(int err, Args&&... args) {
45   throwSystemErrorExplicit(
46       err, to<fbstring>(std::forward<Args>(args)...).c_str());
47 }
48
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)...);
53 }
54
55 // Check a Posix return code (0 on success, error number on error), throw
56 // on error.
57 template <class... Args>
58 void checkPosixError(int err, Args&&... args) {
59   if (UNLIKELY(err != 0)) {
60     throwSystemErrorExplicit(err, std::forward<Args>(args)...);
61   }
62 }
63
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)...);
70   }
71 }
72
73 // Check a traditional Unix return code (-1 and sets errno on error), throw
74 // on error.
75 template <class... Args>
76 void checkUnixError(ssize_t ret, Args&&... args) {
77   if (UNLIKELY(ret == -1)) {
78     throwSystemError(std::forward<Args>(args)...);
79   }
80 }
81
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)...);
86   }
87 }
88
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) {
94   if (UNLIKELY(!fp)) {
95     throwSystemError(std::forward<Args>(args)...);
96   }
97 }
98
99 template <class... Args>
100 void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) {
101   if (UNLIKELY(!fp)) {
102     throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
103   }
104 }
105
106 template <typename E, typename V, typename... Args>
107 void throwOnFail(V&& value, Args&&... args) {
108   if (!value) {
109     throw E(std::forward<Args>(args)...);
110   }
111 }
112
113 /**
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).
116  */
117 #define CHECK_THROW(cond, E) \
118   ::folly::throwOnFail<E>((cond), "Check failed: " #cond)
119
120 }  // namespace folly