Fix SignalHandlerTest with ASAN
[folly.git] / folly / Exception.h
index 4a05535b3f5d828081981f98c5b42ca018c04c57..3985421a883e13125514d4e7be2be678bd28625c 100644 (file)
@@ -35,15 +35,40 @@ namespace folly {
 //
 // The *Explicit functions take an explicit value for errno.
 
+inline std::system_error makeSystemErrorExplicit(int err, const char* msg) {
+  // TODO: The C++ standard indicates that std::generic_category() should be
+  // used for POSIX errno codes.
+  //
+  // We should ideally change this to use std::generic_category() instead of
+  // std::system_category().  However, undertaking this change will require
+  // updating existing call sites that currently catch exceptions thrown by
+  // this code and currently expect std::system_category.
+  return std::system_error(err, std::system_category(), msg);
+}
+
+template <class... Args>
+std::system_error makeSystemErrorExplicit(int err, Args&&... args) {
+  return makeSystemErrorExplicit(
+      err, to<fbstring>(std::forward<Args>(args)...).c_str());
+}
+
+inline std::system_error makeSystemError(const char* msg) {
+  return makeSystemErrorExplicit(errno, msg);
+}
+
+template <class... Args>
+std::system_error makeSystemError(Args&&... args) {
+  return makeSystemErrorExplicit(errno, std::forward<Args>(args)...);
+}
+
 // Helper to throw std::system_error
 [[noreturn]] inline void throwSystemErrorExplicit(int err, const char* msg) {
-  throw std::system_error(err, std::system_category(), msg);
+  throw makeSystemErrorExplicit(err, msg);
 }
 
 template <class... Args>
 [[noreturn]] void throwSystemErrorExplicit(int err, Args&&... args) {
-  throwSystemErrorExplicit(
-      err, to<fbstring>(std::forward<Args>(args)...).c_str());
+  throw makeSystemErrorExplicit(err, std::forward<Args>(args)...);
 }
 
 // Helper to throw std::system_error from errno and components of a string
@@ -114,4 +139,4 @@ void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) {
     }                                  \
   } while (0)
 
-}  // namespace folly
+} // namespace folly