Exception.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2013-present 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. #pragma once
  17. #include <errno.h>
  18. #include <cstdio>
  19. #include <stdexcept>
  20. #include <system_error>
  21. #include <folly/Conv.h>
  22. #include <folly/FBString.h>
  23. #include <folly/Likely.h>
  24. #include <folly/Portability.h>
  25. namespace folly {
  26. // Various helpers to throw appropriate std::system_error exceptions from C
  27. // library errors (returned in errno, as positive return values (many POSIX
  28. // functions), or as negative return values (Linux syscalls))
  29. //
  30. // The *Explicit functions take an explicit value for errno.
  31. inline std::system_error makeSystemErrorExplicit(int err, const char* msg) {
  32. // TODO: The C++ standard indicates that std::generic_category() should be
  33. // used for POSIX errno codes.
  34. //
  35. // We should ideally change this to use std::generic_category() instead of
  36. // std::system_category(). However, undertaking this change will require
  37. // updating existing call sites that currently catch exceptions thrown by
  38. // this code and currently expect std::system_category.
  39. return std::system_error(err, std::system_category(), msg);
  40. }
  41. template <class... Args>
  42. std::system_error makeSystemErrorExplicit(int err, Args&&... args) {
  43. return makeSystemErrorExplicit(
  44. err, to<fbstring>(std::forward<Args>(args)...).c_str());
  45. }
  46. inline std::system_error makeSystemError(const char* msg) {
  47. return makeSystemErrorExplicit(errno, msg);
  48. }
  49. template <class... Args>
  50. std::system_error makeSystemError(Args&&... args) {
  51. return makeSystemErrorExplicit(errno, std::forward<Args>(args)...);
  52. }
  53. // Helper to throw std::system_error
  54. [[noreturn]] inline void throwSystemErrorExplicit(int err, const char* msg) {
  55. throw makeSystemErrorExplicit(err, msg);
  56. }
  57. template <class... Args>
  58. [[noreturn]] void throwSystemErrorExplicit(int err, Args&&... args) {
  59. throw makeSystemErrorExplicit(err, std::forward<Args>(args)...);
  60. }
  61. // Helper to throw std::system_error from errno and components of a string
  62. template <class... Args>
  63. [[noreturn]] void throwSystemError(Args&&... args) {
  64. throwSystemErrorExplicit(errno, std::forward<Args>(args)...);
  65. }
  66. // Check a Posix return code (0 on success, error number on error), throw
  67. // on error.
  68. template <class... Args>
  69. void checkPosixError(int err, Args&&... args) {
  70. if (UNLIKELY(err != 0)) {
  71. throwSystemErrorExplicit(err, std::forward<Args>(args)...);
  72. }
  73. }
  74. // Check a Linux kernel-style return code (>= 0 on success, negative error
  75. // number on error), throw on error.
  76. template <class... Args>
  77. void checkKernelError(ssize_t ret, Args&&... args) {
  78. if (UNLIKELY(ret < 0)) {
  79. throwSystemErrorExplicit(int(-ret), std::forward<Args>(args)...);
  80. }
  81. }
  82. // Check a traditional Unix return code (-1 and sets errno on error), throw
  83. // on error.
  84. template <class... Args>
  85. void checkUnixError(ssize_t ret, Args&&... args) {
  86. if (UNLIKELY(ret == -1)) {
  87. throwSystemError(std::forward<Args>(args)...);
  88. }
  89. }
  90. template <class... Args>
  91. void checkUnixErrorExplicit(ssize_t ret, int savedErrno, Args&&... args) {
  92. if (UNLIKELY(ret == -1)) {
  93. throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
  94. }
  95. }
  96. // Check the return code from a fopen-style function (returns a non-nullptr
  97. // FILE* on success, nullptr on error, sets errno). Works with fopen, fdopen,
  98. // freopen, tmpfile, etc.
  99. template <class... Args>
  100. void checkFopenError(FILE* fp, Args&&... args) {
  101. if (UNLIKELY(!fp)) {
  102. throwSystemError(std::forward<Args>(args)...);
  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. * If cond is not true, raise an exception of type E. E must have a ctor that
  113. * works with const char* (a description of the failure).
  114. */
  115. #define CHECK_THROW(cond, E) \
  116. do { \
  117. if (!(cond)) { \
  118. throw E("Check failed: " #cond); \
  119. } \
  120. } while (0)
  121. } // namespace folly