You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
10 KiB

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