Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: webrtc/api/rtcerror.h

Issue 2692723002: Adding RTCErrorOr class to be used by ORTC APIs. (Closed)
Patch Set: Ading "MoveError". Found a use for it in CL in progress. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
84 // Creates a "no error" error.
85 RTCError() {}
86 explicit RTCError(RTCErrorType type) : type_(type) {}
87 // For performance, prefer using the constructor that takes a const char* if
88 // the message is a static string.
89 RTCError(RTCErrorType type, const char* message)
90 : type_(type), static_message_(message), have_string_message_(false) {}
91 RTCError(RTCErrorType type, std::string&& message)
92 : type_(type), string_message_(message), have_string_message_(true) {}
93
94 // Delete the copy constructor and assignment operator; there aren't any use
95 // cases where you should need to copy an RTCError, as opposed to moving it.
96 // Can revisit this decision if use cases arise in the future.
97 RTCError(const RTCError& other) = delete;
98 RTCError& operator=(const RTCError& other) = delete;
99
100 // Move constructor and move-assignment operator.
101 RTCError(RTCError&& other);
102 RTCError& operator=(RTCError&& other);
103
104 ~RTCError();
105
106 // Identical to default constructed error.
107 //
108 // Preferred over the default constructor for code readability.
109 static RTCError OK();
110
111 // Error type.
112 RTCErrorType type() const { return type_; }
113 void set_type(RTCErrorType type) { type_ = type; }
114
115 // Human-readable message describing the error. Shouldn't be used for
116 // anything but logging/diagnostics, since messages are not guaranteed to be
117 // stable.
118 const char* message() const;
119 // For performance, prefer using the method that takes a const char* if the
120 // message is a static string.
121 void set_message(const char* message);
122 void set_message(std::string&& message);
123
124 // Convenience method for situations where you only care whether or not an
125 // error occurred.
126 bool ok() const { return type_ == RTCErrorType::NONE; }
127
128 private:
129 RTCErrorType type_ = RTCErrorType::NONE;
130 // For performance, we use static strings wherever possible. But in some
131 // cases the error string may need to be constructed, in which case an
132 // std::string is used.
133 union {
134 const char* static_message_ = "";
135 std::string string_message_;
136 };
137 // Whether or not |static_message_| or |string_message_| is being used in the
138 // above union.
139 bool have_string_message_ = false;
140 };
141
142 // Outputs the error as a friendly string. Update this method when adding a new
143 // error type.
144 //
145 // Only intended to be used for logging/disagnostics.
146 std::ostream& operator<<(std::ostream& stream, RTCErrorType error);
147
148 // Helper methods that can be used by implementations to create an error with a
149 // message and log it.
150 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type,
151 const char* message,
152 rtc::LoggingSeverity severity);
153 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type,
154 std::string&& message,
155 rtc::LoggingSeverity severity);
156 // Logs at error level.
157 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type,
158 const char* message);
159 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type,
160 std::string&& message);
161
162 // RTCErrorOr<T> is the union of an RTCError object and a T object. RTCErrorOr
163 // models the concept of an object that is either a usable value, or an error
164 // Status explaining why such a value is not present. To this end RTCErrorOr<T>
165 // does not allow its RTCErrorType value to be RTCErrorType::NONE. This is
166 // enforced by a debug check in most cases.
167 //
168 // The primary use-case for RTCErrorOr<T> is as the return value of a function
169 // which may fail. For example, CreateRtpSender will fail if the parameters
170 // could not be successfully applied at the media engine level, but if
171 // successful will return a unique_ptr to an RtpSender.
172 //
173 // Example client usage for a RTCErrorOr<std::unique_ptr<T>>:
174 //
175 // RTCErrorOr<std::unique_ptr<Foo>> result = FooFactory::MakeNewFoo(arg);
176 // if (result.ok()) {
177 // std::unique_ptr<Foo> foo = result.ConsumeValue();
178 // foo->DoSomethingCool();
179 // } else {
180 // LOG(LS_ERROR) << result.error();
181 // }
182 //
183 // Example factory implementation returning RTCErrorOr<std::unique_ptr<T>>:
184 //
185 // RTCErrorOr<std::unique_ptr<Foo>> FooFactory::MakeNewFoo(int arg) {
186 // if (arg <= 0) {
187 // return RTCError(RTCErrorType::INVALID_RANGE, "Arg must be positive");
188 // } else {
189 // return std::unique_ptr<Foo>(new Foo(arg));
190 // }
191 // }
192 //
193 template <typename T>
194 class RTCErrorOr {
195 // Used to convert between RTCErrorOr<Foo>/RtcErrorOr<Bar>, when an implicit
196 // conversion from Foo to Bar exists.
197 template <typename U>
198 friend class RTCErrorOr;
199
200 public:
201 typedef T element_type;
202
203 // Constructs a new RTCErrorOr with RTCErrorType::INTERNAL_ERROR error. This
204 // is marked 'explicit' to try to catch cases like 'return {};', where people
205 // think RTCErrorOr<std::vector<int>> will be initialized with an empty
206 // vector, instead of a RTCErrorType::INTERNAL_ERROR error.
207 explicit RTCErrorOr() : error_(RTCErrorType::INTERNAL_ERROR) {}
208
209 // Constructs a new RTCErrorOr with the given non-ok error. After calling
210 // this constructor, calls to value() will DCHECK-fail.
211 //
212 // NOTE: Not explicit - we want to use RTCErrorOr<T> as a return
213 // value, so it is convenient and sensible to be able to do 'return
214 // RTCError(...)' when the return type is RTCErrorOr<T>.
215 //
216 // REQUIRES: !error.ok(). This requirement is DCHECKed.
217 RTCErrorOr(RTCError&& error) : error_(std::move(error)) {
218 RTC_DCHECK(!error.ok());
219 }
220
221 // Constructs a new RTCErrorOr with the given value. After calling this
222 // constructor, calls to value() will succeed, and calls to error() will
223 // return a default-constructed RTCError.
224 //
225 // NOTE: Not explicit - we want to use RTCErrorOr<T> as a return type
226 // so it is convenient and sensible to be able to do 'return T()'
227 // when the return type is RTCErrorOr<T>.
228 RTCErrorOr(T value) : value_(std::move(value)) {}
229
230 // Delete the copy constructor and assignment operator; there aren't any use
231 // cases where you should need to copy an RTCErrorOr, as opposed to moving
232 // it. Can revisit this decision if use cases arise in the future.
233 RTCErrorOr(const RTCErrorOr& other) = delete;
234 RTCErrorOr& operator=(const RTCErrorOr& other) = delete;
235
236 // Move constructor and move-assignment operator.
237 RTCErrorOr(RTCErrorOr&& other) = default;
238 RTCErrorOr& operator=(RTCErrorOr&& other) = default;
239
240 // Conversion constructor and assignment operator; T must be copy or move
241 // constructible from U.
242 template <typename U>
243 RTCErrorOr(RTCErrorOr<U> other)
244 : error_(std::move(other.error_)), value_(std::move(other.value_)) {}
245 template <typename U>
246 RTCErrorOr& operator=(RTCErrorOr<U> other) {
247 error_ = std::move(other.error_);
248 value_ = std::move(other.value_);
249 return *this;
250 }
251
252 // Returns a reference to our error. If this contains a T, then returns
253 // default-constructed RTCError.
254 const RTCError& error() const { return error_; }
255
256 // Moves the error. Can be useful if, say "CreateFoo" returns an
257 // RTCErrorOr<Foo>, and internally calls "CreateBar" which returns an
258 // RTCErrorOr<Bar>, and wants to forward the error up the stack.
259 RTCError MoveError() { return std::move(error_); }
260
261 // Returns this->error().ok()
262 bool ok() const { return error_.ok(); }
263
264 // Returns a reference to our current value, or DCHECK-fails if !this->ok().
265 //
266 // Can be convenient for the implementation; for example, a method may want
267 // to access the value in some way before returning it to the next method on
268 // the stack.
269 const T& value() const {
270 RTC_DCHECK(ok());
271 return value_;
272 }
273 T& value() {
274 RTC_DCHECK(ok());
275 return value_;
276 }
277
278 // Moves our current value out of this object and returns it, or DCHECK-fails
279 // if !this->ok().
280 T MoveValue() {
281 RTC_DCHECK(ok());
282 return std::move(value_);
283 }
284
285 private:
286 RTCError error_;
287 T value_;
288 };
289
290 } // namespace webrtc
291
292 #endif // WEBRTC_API_RTCERROR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698