2 * Copyright 2014 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.
17 #ifndef FOLLY_EXCEPTION_H_
18 #define FOLLY_EXCEPTION_H_
24 #include <system_error>
26 #include <folly/Conv.h>
27 #include <folly/FBString.h>
28 #include <folly/Likely.h>
29 #include <folly/Portability.h>
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))
37 // The *Explicit functions take an explicit value for errno.
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);
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());
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)...);
61 // Check a Posix return code (0 on success, error number on error), throw
63 template <class... Args>
64 void checkPosixError(int err, Args&&... args) {
65 if (UNLIKELY(err != 0)) {
66 throwSystemErrorExplicit(err, std::forward<Args>(args)...);
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)...);
79 // Check a traditional Unix return code (-1 and sets errno on error), throw
81 template <class... Args>
82 void checkUnixError(ssize_t ret, Args&&... args) {
83 if (UNLIKELY(ret == -1)) {
84 throwSystemError(std::forward<Args>(args)...);
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)...);
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) {
101 throwSystemError(std::forward<Args>(args)...);
105 template <class... Args>
106 void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) {
108 throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
112 template <typename E, typename V, typename... Args>
113 void throwOnFail(V&& value, Args&&... args) {
115 throw E(std::forward<Args>(args)...);
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).
123 #define CHECK_THROW(cond, E) \
124 ::folly::throwOnFail<E>((cond), "Check failed: " #cond)
128 #endif /* FOLLY_EXCEPTION_H_ */