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

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

Issue 2692723002: Adding RTCErrorOr class to be used by ORTC APIs. (Closed)
Patch Set: Use const char* for error message. 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
« no previous file with comments | « webrtc/api/peerconnectioninterface.h ('k') | webrtc/api/rtcerror.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
kwiberg-webrtc 2017/02/14 09:51:49 If this sort of thing is a consideration, you may
tommi 2017/02/14 09:55:56 this is a consideration because there's no need to
kwiberg-webrtc 2017/02/14 11:37:25 Then I'd suggest having *only* the const char* and
tommi 2017/02/14 11:58:58 That sounds good to me. I may have mentioned it a
Taylor Brandstetter 2017/02/15 02:22:23 Done.
89 RTCError(RTCErrorType type, const char* message)
90 : type_(type), message_(message), static_message_(true) {}
91 RTCError(RTCErrorType type, const std::string& message)
92 : type_(type), message_(message), static_message_(false) {}
93 ~RTCError();
94
95 // Identical to default constructed error.
96 //
97 // Preferred over the default constructor for code readability, and reducing
98 // unnecessary copies.
99 static const RTCError& OK();
100
101 // Error type.
102 RTCErrorType type() const { return type_; }
103 void set_type(RTCErrorType type) { type_ = type; }
104
105 // Human-readable message describing the error. Shouldn't be used for
106 // anything but logging/diagnostics, since messages are not guaranteed to be
107 // stable.
108 const char* message() const;
109 // For performance, prefer using the method that takes a const char* if the
110 // message is a static string.
111 void set_message(const char* message);
112 void set_message(const std::string& message);
113
114 // Convenience method for situations where you only care whether or not an
115 // error occurred.
116 bool ok() const { return type_ == RTCErrorType::NONE; }
117
118 private:
119 RTCErrorType type_ = RTCErrorType::NONE;
120 // For performance, we use static strings wherever possible. But in some
121 // cases the error string may need to be constructed, in which case an
122 // std::string is used.
123 union MessageUnion {
124 MessageUnion() = default;
125 MessageUnion(const char* message) : static_(message) {}
126 MessageUnion(const std::string& message) : str_(new std::string(message)) {}
127 const char* static_ = "";
128 std::string* str_;
129 } message_;
130 // Whether or not static_ or str_ is being used in the above union.
131 bool static_message_ = true;
132 };
133
134 // Outputs the error as a friendly string. Update this method when adding a new
135 // error type.
136 //
137 // Only intended to be used for logging/disagnostics.
138 std::ostream& operator<<(std::ostream& stream, RTCErrorType error);
139
140 // Helper methods that can be used by implementations to create an error with a
141 // message and log it.
142 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type,
143 const char* message,
144 rtc::LoggingSeverity severity);
145 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type,
146 const std::string& message,
147 rtc::LoggingSeverity severity);
148 // Logs at error level.
149 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type,
150 const char* message);
151 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type,
152 const std::string& message);
153
154 // RTCErrorOr<T> is the union of an RTCError object and a T object. RTCErrorOr
155 // models the concept of an object that is either a usable value, or an error
156 // Status explaining why such a value is not present. To this end RTCErrorOr<T>
157 // does not allow its RTCErrorType value to be RTCErrorType::NONE. This is
158 // enforced by a debug check in most cases.
159 //
160 // The primary use-case for RTCErrorOr<T> is as the return value of a function
161 // which may fail. For example, CreateRtpSender will fail if the parameters
162 // could not be successfully applied at the media engine level, but if
163 // successful will return a unique_ptr to an RtpSender.
164 //
165 // Example client usage for a RTCErrorOr<std::unique_ptr<T>>:
166 //
167 // RTCErrorOr<std::unique_ptr<Foo>> result = FooFactory::MakeNewFoo(arg);
168 // if (result.ok()) {
169 // std::unique_ptr<Foo> foo = result.ConsumeValue();
170 // foo->DoSomethingCool();
171 // } else {
172 // LOG(LS_ERROR) << result.error();
173 // }
174 //
175 // Example factory implementation returning RTCErrorOr<std::unique_ptr<T>>:
176 //
177 // RTCErrorOr<std::unique_ptr<Foo>> FooFactory::MakeNewFoo(int arg) {
178 // if (arg <= 0) {
179 // return RTCError(RTCErrorType::INVALID_RANGE, "Arg must be positive");
180 // } else {
181 // return std::unique_ptr<Foo>(new Foo(arg));
182 // }
183 // }
184 //
185 template <typename T>
186 class RTCErrorOr {
187 // Used to convert between RTCErrorOr<Foo>/RtcErrorOr<Bar>, when an implicit
188 // conversion from Foo to Bar exists.
189 template <typename U>
190 friend class RTCErrorOr;
191
192 public:
193 typedef T element_type;
194
195 // Constructs a new RTCErrorOr with RTCErrorType::NONE error. This is marked
196 // 'explicit' to try to catch cases like 'return {};', where people think
197 // RTCErrorOr<std::vector<int>> will be initialized with an empty vector,
198 // instead of a RTCErrorType::NONE error.
199 explicit RTCErrorOr() = default;
200
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(const RTCError& error) : error_(error) { RTC_DCHECK(!error.ok()); }
210 RTCErrorOr(RTCError&& error) : error_(std::move(error)) {
211 RTC_DCHECK(!error.ok());
212 }
213
214 // Constructs a new RTCErrorOr with the given value. After calling this
215 // constructor, calls to value() will succeed, and calls to error() will
216 // return a default-constructed RTCError.
217 //
218 // NOTE: Not explicit - we want to use RTCErrorOr<T> as a return type
219 // so it is convenient and sensible to be able to do 'return T()'
220 // when the return type is RTCErrorOr<T>.
221 RTCErrorOr(T value) : value_(std::move(value)) {}
222
223 // Delete the copy constructor and assignment operator; there aren't any use
224 // cases where you should need to copy an RTCErrorOr, as opposed to moving
225 // it. Can revisit this decision if use cases arise in the future.
226 RTCErrorOr(const RTCErrorOr& other) = delete;
227 RTCErrorOr& operator=(const RTCErrorOr& other) = delete;
228
229 // Move constructor and move-assignment operator.
230 RTCErrorOr(RTCErrorOr&& other) = default;
231 RTCErrorOr& operator=(RTCErrorOr&& other) = default;
232
233 // Conversion constructor and assignment operator; T must be copy or move
234 // constructible from U.
235 template <typename U>
236 RTCErrorOr(RTCErrorOr<U> other)
237 : error_(std::move(other.error_)), value_(std::move(other.value_)) {}
238 template <typename U>
239 RTCErrorOr& operator=(RTCErrorOr<U> other) {
240 error_ = std::move(other.error_);
241 value_ = std::move(other.value_);
242 return *this;
243 }
244
245 // Returns a reference to our error. If this contains a T, then returns
246 // default-constructed RTCError.
247 const RTCError& error() const { return error_; }
248
249 // Returns this->error().ok()
250 bool ok() const { return error_.ok(); }
251
252 // Returns a reference to our current value, or DCHECK-fails if !this->ok().
253 //
254 // Can be convenient for the implementation; for example, a method may want
255 // to access the value in some way before returning it to the next method on
256 // the stack.
257 const T& value() const {
258 RTC_DCHECK(ok());
259 return value_;
260 }
261 T& value() {
262 RTC_DCHECK(ok());
263 return value_;
264 }
265
266 // Moves our current value out of this object and returns it, or DCHECK-fails
267 // if !this->ok().
268 T MoveValue() {
269 RTC_DCHECK(ok());
270 return std::move(value_);
271 }
272
273 private:
274 RTCError error_;
275 T value_;
276 };
277
278 } // namespace webrtc
279
280 #endif // WEBRTC_API_RTCERROR_H_
OLDNEW
« no previous file with comments | « webrtc/api/peerconnectioninterface.h ('k') | webrtc/api/rtcerror.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698