Use C++'s standardized [[noreturn]] attribute
[folly.git] / folly / Exception.h
1 /*
2  * Copyright 2016 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 [[noreturn]] inline void throwSystemErrorExplicit(int err, const char* msg) {
41   throw std::system_error(err, std::system_category(), msg);
42 }
43
44 template <class... Args>
45 [[noreturn]] void throwSystemErrorExplicit(int err, Args&&... args) {
46   throwSystemErrorExplicit(
47       err, to<fbstring>(std::forward<Args>(args)...).c_str());
48 }
49
50 // Helper to throw std::system_error from errno and components of a string
51 template <class... Args>
52 [[noreturn]] void throwSystemError(Args&&... args) {
53   throwSystemErrorExplicit(errno, std::forward<Args>(args)...);
54 }
55
56 // Check a Posix return code (0 on success, error number on error), throw
57 // on error.
58 template <class... Args>
59 void checkPosixError(int err, Args&&... args) {
60   if (UNLIKELY(err != 0)) {
61     throwSystemErrorExplicit(err, std::forward<Args>(args)...);
62   }
63 }
64
65 // Check a Linux kernel-style return code (>= 0 on success, negative error
66 // number on error), throw on error.
67 template <class... Args>
68 void checkKernelError(ssize_t ret, Args&&... args) {
69   if (UNLIKELY(ret < 0)) {
70     throwSystemErrorExplicit(-ret, std::forward<Args>(args)...);
71   }
72 }
73
74 // Check a traditional Unix return code (-1 and sets errno on error), throw
75 // on error.
76 template <class... Args>
77 void checkUnixError(ssize_t ret, Args&&... args) {
78   if (UNLIKELY(ret == -1)) {
79     throwSystemError(std::forward<Args>(args)...);
80   }
81 }
82
83 template <class... Args>
84 void checkUnixErrorExplicit(ssize_t ret, int savedErrno, Args&&... args) {
85   if (UNLIKELY(ret == -1)) {
86     throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
87   }
88 }
89
90 // Check the return code from a fopen-style function (returns a non-nullptr
91 // FILE* on success, nullptr on error, sets errno).  Works with fopen, fdopen,
92 // freopen, tmpfile, etc.
93 template <class... Args>
94 void checkFopenError(FILE* fp, Args&&... args) {
95   if (UNLIKELY(!fp)) {
96     throwSystemError(std::forward<Args>(args)...);
97   }
98 }
99
100 template <class... Args>
101 void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) {
102   if (UNLIKELY(!fp)) {
103     throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
104   }
105 }
106
107 template <typename E, typename V, typename... Args>
108 void throwOnFail(V&& value, Args&&... args) {
109   if (!value) {
110     throw E(std::forward<Args>(args)...);
111   }
112 }
113
114 /**
115  * If cond is not true, raise an exception of type E.  E must have a ctor that
116  * works with const char* (a description of the failure).
117  */
118 #define CHECK_THROW(cond, E) \
119   ::folly::throwOnFail<E>((cond), "Check failed: " #cond)
120
121 }  // namespace folly
122
123 #endif /* FOLLY_EXCEPTION_H_ */