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