rtc_error.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef API_RTC_ERROR_H_
  11. #define API_RTC_ERROR_H_
  12. #ifdef UNIT_TEST
  13. #include <ostream>
  14. #endif // UNIT_TEST
  15. #include <string>
  16. #include <utility> // For std::move.
  17. #include "absl/types/optional.h"
  18. #include "rtc_base/checks.h"
  19. #include "rtc_base/logging.h"
  20. #include "rtc_base/system/rtc_export.h"
  21. namespace webrtc {
  22. // Enumeration to represent distinct classes of errors that an application
  23. // may wish to act upon differently. These roughly map to DOMExceptions or
  24. // RTCError "errorDetailEnum" values in the web API, as described in the
  25. // comments below.
  26. enum class RTCErrorType {
  27. // No error.
  28. NONE,
  29. // An operation is valid, but currently unsupported.
  30. // Maps to OperationError DOMException.
  31. UNSUPPORTED_OPERATION,
  32. // A supplied parameter is valid, but currently unsupported.
  33. // Maps to OperationError DOMException.
  34. UNSUPPORTED_PARAMETER,
  35. // General error indicating that a supplied parameter is invalid.
  36. // Maps to InvalidAccessError or TypeError DOMException depending on context.
  37. INVALID_PARAMETER,
  38. // Slightly more specific than INVALID_PARAMETER; a parameter's value was
  39. // outside the allowed range.
  40. // Maps to RangeError DOMException.
  41. INVALID_RANGE,
  42. // Slightly more specific than INVALID_PARAMETER; an error occurred while
  43. // parsing string input.
  44. // Maps to SyntaxError DOMException.
  45. SYNTAX_ERROR,
  46. // The object does not support this operation in its current state.
  47. // Maps to InvalidStateError DOMException.
  48. INVALID_STATE,
  49. // An attempt was made to modify the object in an invalid way.
  50. // Maps to InvalidModificationError DOMException.
  51. INVALID_MODIFICATION,
  52. // An error occurred within an underlying network protocol.
  53. // Maps to NetworkError DOMException.
  54. NETWORK_ERROR,
  55. // Some resource has been exhausted; file handles, hardware resources, ports,
  56. // etc.
  57. // Maps to OperationError DOMException.
  58. RESOURCE_EXHAUSTED,
  59. // The operation failed due to an internal error.
  60. // Maps to OperationError DOMException.
  61. INTERNAL_ERROR,
  62. // An error occured that has additional data.
  63. // The additional data is specified in
  64. // https://w3c.github.io/webrtc-pc/#rtcerror-interface
  65. // Maps to RTCError DOMException.
  66. OPERATION_ERROR_WITH_DATA,
  67. };
  68. // Detail information, showing what further information should be present.
  69. // https://w3c.github.io/webrtc-pc/#rtcerrordetailtype-enum
  70. enum class RTCErrorDetailType {
  71. NONE,
  72. DATA_CHANNEL_FAILURE,
  73. DTLS_FAILURE,
  74. FINGERPRINT_FAILURE,
  75. SCTP_FAILURE,
  76. SDP_SYNTAX_ERROR,
  77. HARDWARE_ENCODER_NOT_AVAILABLE,
  78. HARDWARE_ENCODER_ERROR,
  79. };
  80. // Roughly corresponds to RTCError in the web api. Holds an error type, a
  81. // message, and possibly additional information specific to that error.
  82. //
  83. // Doesn't contain anything beyond a type and message now, but will in the
  84. // future as more errors are implemented.
  85. class RTC_EXPORT RTCError {
  86. public:
  87. // Constructors.
  88. // Creates a "no error" error.
  89. RTCError() {}
  90. explicit RTCError(RTCErrorType type) : type_(type) {}
  91. RTCError(RTCErrorType type, std::string message)
  92. : type_(type), message_(std::move(message)) {}
  93. // In many use cases, it is better to use move than copy,
  94. // but copy and assignment are provided for those cases that need it.
  95. // Note that this has extra overhead because it copies strings.
  96. RTCError(const RTCError& other) = default;
  97. RTCError(RTCError&&) = default;
  98. RTCError& operator=(const RTCError& other) = default;
  99. RTCError& operator=(RTCError&&) = default;
  100. // Identical to default constructed error.
  101. //
  102. // Preferred over the default constructor for code readability.
  103. static RTCError OK();
  104. // Error type.
  105. RTCErrorType type() const { return type_; }
  106. void set_type(RTCErrorType type) { type_ = type; }
  107. // Human-readable message describing the error. Shouldn't be used for
  108. // anything but logging/diagnostics, since messages are not guaranteed to be
  109. // stable.
  110. const char* message() const;
  111. void set_message(std::string message);
  112. RTCErrorDetailType error_detail() const { return error_detail_; }
  113. void set_error_detail(RTCErrorDetailType detail) { error_detail_ = detail; }
  114. absl::optional<uint16_t> sctp_cause_code() const { return sctp_cause_code_; }
  115. void set_sctp_cause_code(uint16_t cause_code) {
  116. sctp_cause_code_ = cause_code;
  117. }
  118. // Convenience method for situations where you only care whether or not an
  119. // error occurred.
  120. bool ok() const { return type_ == RTCErrorType::NONE; }
  121. private:
  122. RTCErrorType type_ = RTCErrorType::NONE;
  123. std::string message_;
  124. RTCErrorDetailType error_detail_ = RTCErrorDetailType::NONE;
  125. absl::optional<uint16_t> sctp_cause_code_;
  126. };
  127. // Outputs the error as a friendly string. Update this method when adding a new
  128. // error type.
  129. //
  130. // Only intended to be used for logging/diagnostics. The returned char* points
  131. // to literal string that lives for the whole duration of the program.
  132. RTC_EXPORT const char* ToString(RTCErrorType error);
  133. RTC_EXPORT const char* ToString(RTCErrorDetailType error);
  134. #ifdef UNIT_TEST
  135. inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
  136. std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
  137. RTCErrorType error) {
  138. return stream << ToString(error);
  139. }
  140. inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
  141. std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
  142. RTCErrorDetailType error) {
  143. return stream << ToString(error);
  144. }
  145. #endif // UNIT_TEST
  146. // Helper macro that can be used by implementations to create an error with a
  147. // message and log it. |message| should be a string literal or movable
  148. // std::string.
  149. #define LOG_AND_RETURN_ERROR_EX(type, message, severity) \
  150. { \
  151. RTC_DCHECK(type != RTCErrorType::NONE); \
  152. RTC_LOG(severity) << message << " (" << ToString(type) << ")"; \
  153. return webrtc::RTCError(type, message); \
  154. }
  155. #define LOG_AND_RETURN_ERROR(type, message) \
  156. LOG_AND_RETURN_ERROR_EX(type, message, LS_ERROR)
  157. // RTCErrorOr<T> is the union of an RTCError object and a T object. RTCErrorOr
  158. // models the concept of an object that is either a usable value, or an error
  159. // Status explaining why such a value is not present. To this end RTCErrorOr<T>
  160. // does not allow its RTCErrorType value to be RTCErrorType::NONE. This is
  161. // enforced by a debug check in most cases.
  162. //
  163. // The primary use-case for RTCErrorOr<T> is as the return value of a function
  164. // which may fail. For example, CreateRtpSender will fail if the parameters
  165. // could not be successfully applied at the media engine level, but if
  166. // successful will return a unique_ptr to an RtpSender.
  167. //
  168. // Example client usage for a RTCErrorOr<std::unique_ptr<T>>:
  169. //
  170. // RTCErrorOr<std::unique_ptr<Foo>> result = FooFactory::MakeNewFoo(arg);
  171. // if (result.ok()) {
  172. // std::unique_ptr<Foo> foo = result.ConsumeValue();
  173. // foo->DoSomethingCool();
  174. // } else {
  175. // RTC_LOG(LS_ERROR) << result.error();
  176. // }
  177. //
  178. // Example factory implementation returning RTCErrorOr<std::unique_ptr<T>>:
  179. //
  180. // RTCErrorOr<std::unique_ptr<Foo>> FooFactory::MakeNewFoo(int arg) {
  181. // if (arg <= 0) {
  182. // return RTCError(RTCErrorType::INVALID_RANGE, "Arg must be positive");
  183. // } else {
  184. // return std::unique_ptr<Foo>(new Foo(arg));
  185. // }
  186. // }
  187. //
  188. template <typename T>
  189. class RTCErrorOr {
  190. // Used to convert between RTCErrorOr<Foo>/RtcErrorOr<Bar>, when an implicit
  191. // conversion from Foo to Bar exists.
  192. template <typename U>
  193. friend class RTCErrorOr;
  194. public:
  195. typedef T element_type;
  196. // Constructs a new RTCErrorOr with RTCErrorType::INTERNAL_ERROR error. This
  197. // is marked 'explicit' to try to catch cases like 'return {};', where people
  198. // think RTCErrorOr<std::vector<int>> will be initialized with an empty
  199. // vector, instead of a RTCErrorType::INTERNAL_ERROR error.
  200. RTCErrorOr() : error_(RTCErrorType::INTERNAL_ERROR) {}
  201. // Constructs a new RTCErrorOr with the given non-ok error. After calling
  202. // this constructor, calls to value() will DCHECK-fail.
  203. //
  204. // NOTE: Not explicit - we want to use RTCErrorOr<T> as a return
  205. // value, so it is convenient and sensible to be able to do 'return
  206. // RTCError(...)' when the return type is RTCErrorOr<T>.
  207. //
  208. // REQUIRES: !error.ok(). This requirement is DCHECKed.
  209. RTCErrorOr(RTCError&& error) : error_(std::move(error)) { // NOLINT
  210. RTC_DCHECK(!error.ok());
  211. }
  212. // Constructs a new RTCErrorOr with the given value. After calling this
  213. // constructor, calls to value() will succeed, and calls to error() will
  214. // return a default-constructed RTCError.
  215. //
  216. // NOTE: Not explicit - we want to use RTCErrorOr<T> as a return type
  217. // so it is convenient and sensible to be able to do 'return T()'
  218. // when the return type is RTCErrorOr<T>.
  219. RTCErrorOr(const T& value) : value_(value) {} // NOLINT
  220. RTCErrorOr(T&& value) : value_(std::move(value)) {} // NOLINT
  221. // Delete the copy constructor and assignment operator; there aren't any use
  222. // cases where you should need to copy an RTCErrorOr, as opposed to moving
  223. // it. Can revisit this decision if use cases arise in the future.
  224. RTCErrorOr(const RTCErrorOr& other) = delete;
  225. RTCErrorOr& operator=(const RTCErrorOr& other) = delete;
  226. // Move constructor and move-assignment operator.
  227. //
  228. // Visual Studio doesn't support "= default" with move constructors or
  229. // assignment operators (even though they compile, they segfault), so define
  230. // them explicitly.
  231. RTCErrorOr(RTCErrorOr&& other)
  232. : error_(std::move(other.error_)), value_(std::move(other.value_)) {}
  233. RTCErrorOr& operator=(RTCErrorOr&& other) {
  234. error_ = std::move(other.error_);
  235. value_ = std::move(other.value_);
  236. return *this;
  237. }
  238. // Conversion constructor and assignment operator; T must be copy or move
  239. // constructible from U.
  240. template <typename U>
  241. RTCErrorOr(RTCErrorOr<U> other) // NOLINT
  242. : error_(std::move(other.error_)), value_(std::move(other.value_)) {}
  243. template <typename U>
  244. RTCErrorOr& operator=(RTCErrorOr<U> other) {
  245. error_ = std::move(other.error_);
  246. value_ = std::move(other.value_);
  247. return *this;
  248. }
  249. // Returns a reference to our error. If this contains a T, then returns
  250. // default-constructed RTCError.
  251. const RTCError& error() const { return error_; }
  252. // Moves the error. Can be useful if, say "CreateFoo" returns an
  253. // RTCErrorOr<Foo>, and internally calls "CreateBar" which returns an
  254. // RTCErrorOr<Bar>, and wants to forward the error up the stack.
  255. RTCError MoveError() { return std::move(error_); }
  256. // Returns this->error().ok()
  257. bool ok() const { return error_.ok(); }
  258. // Returns a reference to our current value, or DCHECK-fails if !this->ok().
  259. //
  260. // Can be convenient for the implementation; for example, a method may want
  261. // to access the value in some way before returning it to the next method on
  262. // the stack.
  263. const T& value() const {
  264. RTC_DCHECK(ok());
  265. return value_;
  266. }
  267. T& value() {
  268. RTC_DCHECK(ok());
  269. return value_;
  270. }
  271. // Moves our current value out of this object and returns it, or DCHECK-fails
  272. // if !this->ok().
  273. T MoveValue() {
  274. RTC_DCHECK(ok());
  275. return std::move(value_);
  276. }
  277. private:
  278. RTCError error_;
  279. T value_;
  280. };
  281. } // namespace webrtc
  282. #endif // API_RTC_ERROR_H_