fix fbstring move assignment operator
[folly.git] / folly / Exception.h
1 /*
2  * Copyright 2013 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 <stdexcept>
23 #include <system_error>
24
25 #include "folly/Conv.h"
26 #include "folly/Likely.h"
27 #include "folly/Portability.h"
28
29 namespace folly {
30
31 // Helper to throw std::system_error
32 void throwSystemError(int err, const char* msg) FOLLY_NORETURN;
33 inline void throwSystemError(int err, const char* msg) {
34   throw std::system_error(err, std::system_category(), msg);
35 }
36
37 // Helper to throw std::system_error from errno
38 void throwSystemError(const char* msg) FOLLY_NORETURN;
39 inline void throwSystemError(const char* msg) {
40   throwSystemError(errno, msg);
41 }
42
43 // Helper to throw std::system_error from errno and components of a string
44 template <class... Args>
45 void throwSystemError(Args... args) FOLLY_NORETURN;
46 template <class... Args>
47 inline void throwSystemError(Args... args) {
48   throwSystemError(errno, folly::to<std::string>(args...));
49 }
50
51 // Check a Posix return code (0 on success, error number on error), throw
52 // on error.
53 inline void checkPosixError(int err, const char* msg) {
54   if (UNLIKELY(err != 0)) {
55     throwSystemError(err, msg);
56   }
57 }
58
59 // Check a Linux kernel-style return code (>= 0 on success, negative error
60 // number on error), throw on error.
61 inline void checkKernelError(ssize_t ret, const char* msg) {
62   if (UNLIKELY(ret < 0)) {
63     throwSystemError(-ret, msg);
64   }
65 }
66
67 // Check a traditional Unix return code (-1 and sets errno on error), throw
68 // on error.
69 inline void checkUnixError(ssize_t ret, const char* msg) {
70   if (UNLIKELY(ret == -1)) {
71     throwSystemError(msg);
72   }
73 }
74 inline void checkUnixError(ssize_t ret, int savedErrno, const char* msg) {
75   if (UNLIKELY(ret == -1)) {
76     throwSystemError(savedErrno, msg);
77   }
78 }
79
80 }  // namespace folly
81
82 #endif /* FOLLY_EXCEPTION_H_ */
83