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