28188c639ae8afc972d2a710bed1c809fd292c29
[folly.git] / folly / Exception.h
1 /*
2  * Copyright 2015 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 #ifndef FOLLY_EXCEPTION_H_
18 #define FOLLY_EXCEPTION_H_
19
20 #include <errno.h>
21
22 #include <cstdio>
23 #include <stdexcept>
24 #include <system_error>
25
26 #include <folly/Conv.h>
27 #include <folly/FBString.h>
28 #include <folly/Likely.h>
29 #include <folly/Portability.h>
30
31 namespace folly {
32
33 // Various helpers to throw appropriate std::system_error exceptions from C
34 // library errors (returned in errno, as positive return values (many POSIX
35 // functions), or as negative return values (Linux syscalls))
36 //
37 // The *Explicit functions take an explicit value for errno.
38
39 // Helper to throw std::system_error
40 FOLLY_NORETURN void throwSystemErrorExplicit(int err, const char*);
41 inline void throwSystemErrorExplicit(int err, const char* msg) {
42   throw std::system_error(err, std::system_category(), msg);
43 }
44
45 template <class... Args>
46 FOLLY_NORETURN void throwSystemErrorExplicit(int, Args&&... args);
47 template <class... Args>
48 void throwSystemErrorExplicit(int err, Args&&... args) {
49   throwSystemErrorExplicit(
50       err, to<fbstring>(std::forward<Args>(args)...).c_str());
51 }
52
53 // Helper to throw std::system_error from errno and components of a string
54 template <class... Args>
55 FOLLY_NORETURN void throwSystemError(Args&&... args);
56 template <class... Args>
57 void throwSystemError(Args&&... args) {
58   throwSystemErrorExplicit(errno, std::forward<Args>(args)...);
59 }
60
61 // Check a Posix return code (0 on success, error number on error), throw
62 // on error.
63 template <class... Args>
64 void checkPosixError(int err, Args&&... args) {
65   if (UNLIKELY(err != 0)) {
66     throwSystemErrorExplicit(err, std::forward<Args>(args)...);
67   }
68 }
69
70 // Check a Linux kernel-style return code (>= 0 on success, negative error
71 // number on error), throw on error.
72 template <class... Args>
73 void checkKernelError(ssize_t ret, Args&&... args) {
74   if (UNLIKELY(ret < 0)) {
75     throwSystemErrorExplicit(-ret, std::forward<Args>(args)...);
76   }
77 }
78
79 // Check a traditional Unix return code (-1 and sets errno on error), throw
80 // on error.
81 template <class... Args>
82 void checkUnixError(ssize_t ret, Args&&... args) {
83   if (UNLIKELY(ret == -1)) {
84     throwSystemError(std::forward<Args>(args)...);
85   }
86 }
87
88 template <class... Args>
89 void checkUnixErrorExplicit(ssize_t ret, int savedErrno, Args&&... args) {
90   if (UNLIKELY(ret == -1)) {
91     throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
92   }
93 }
94
95 // Check the return code from a fopen-style function (returns a non-nullptr
96 // FILE* on success, nullptr on error, sets errno).  Works with fopen, fdopen,
97 // freopen, tmpfile, etc.
98 template <class... Args>
99 void checkFopenError(FILE* fp, Args&&... args) {
100   if (UNLIKELY(!fp)) {
101     throwSystemError(std::forward<Args>(args)...);
102   }
103 }
104
105 template <class... Args>
106 void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) {
107   if (UNLIKELY(!fp)) {
108     throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
109   }
110 }
111
112 template <typename E, typename V, typename... Args>
113 void throwOnFail(V&& value, Args&&... args) {
114   if (!value) {
115     throw E(std::forward<Args>(args)...);
116   }
117 }
118
119 /**
120  * If cond is not true, raise an exception of type E.  E must have a ctor that
121  * works with const char* (a description of the failure).
122  */
123 #define CHECK_THROW(cond, E) \
124   ::folly::throwOnFail<E>((cond), "Check failed: " #cond)
125
126 }  // namespace folly
127
128 #endif /* FOLLY_EXCEPTION_H_ */