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

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

Issue 2692723002: Adding RTCErrorOr class to be used by ORTC APIs. (Closed)
Patch Set: Updates based on tommi's comments 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() = default;
86 explicit RTCError(RTCErrorType type) : type_(type) {}
87 RTCError(RTCErrorType type, const std::string& message)
88 : type_(type), message_(message) {}
89
90 // Identical to default constructed error.
91 //
92 // Preferred over the default constructor for code readability, and reducing
93 // unnecessary copies.
94 static const RTCError& OK();
95
96 // Error type.
97 RTCErrorType type() const { return type_; }
98 void set_type(RTCErrorType type) { type_ = type; }
99
100 // Human-readable message describing the error. Shouldn't be used for
101 // anything but logging/diagnostics, since messages are not guaranteed to be
102 // stable.
103 const std::string& message() const { return message_; }
104 void set_message(const std::string& message) { message_ = message; }
tommi 2017/02/13 18:17:14 is this method necessary? It could have benefits t
Taylor Brandstetter 2017/02/13 18:44:18 The PeerConnection methods that take an "RTCError*
105
106 // Convenience method for situations where you only care whether or not an
107 // error occurred.
108 bool ok() const { return type_ == RTCErrorType::NONE; }
109
110 private:
111 RTCErrorType type_ = RTCErrorType::NONE;
112 std::string message_;
tommi 2017/02/13 12:10:01 Can we avoid allocating a string for every error?
Taylor Brandstetter 2017/02/13 18:03:53 No, there are cases where I want to build the stri
tommi 2017/02/13 18:17:14 I think the answer to "Won't some be either empty
Taylor Brandstetter 2017/02/13 18:44:17 Oh, I didn't see "some", sorry. Hmm. But if "mess
113 };
114
115 // Outputs the error as a friendly string. Update this method when adding a new
116 // error type.
117 //
118 // Only intended to be used for logging/disagnostics.
119 std::ostream& operator<<(std::ostream& stream, RTCErrorType error);
120
121 // Helper method that can be used by implementations to create an error with a
122 // message and log it.
123 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type,
124 const std::string& message,
125 rtc::LoggingSeverity severity);
126
127 // Logs at error level.
128 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type,
129 const std::string& message);
130
131 // RTCErrorOr<T> is the union of an RTCError object and a T object. RTCErrorOr
132 // models the concept of an object that is either a usable value, or an error
133 // Status explaining why such a value is not present. To this end RTCErrorOr<T>
134 // does not allow its RTCErrorType value to be RTCErrorType::NONE. This is
135 // enforced by a debug check in most cases.
136 //
137 // The primary use-case for RTCErrorOr<T> is as the return value of a function
138 // which may fail. For example, CreateRtpSender will fail if the parameters
139 // could not be successfully applied at the media engine level, but if
140 // successful will return a unique_ptr to an RtpSender.
141 //
142 // Example client usage for a RTCErrorOr<std::unique_ptr<T>>:
143 //
144 // RTCErrorOr<std::unique_ptr<Foo>> result = FooFactory::MakeNewFoo(arg);
145 // if (result.ok()) {
146 // std::unique_ptr<Foo> foo = result.ConsumeValue();
147 // foo->DoSomethingCool();
148 // } else {
149 // LOG(LS_ERROR) << result.error();
150 // }
151 //
152 // Example factory implementation returning RTCErrorOr<std::unique_ptr<T>>:
153 //
154 // RTCErrorOr<std::unique_ptr<Foo>> FooFactory::MakeNewFoo(int arg) {
155 // if (arg <= 0) {
156 // return RTCError(RTCErrorType::INVALID_RANGE, "Arg must be positive");
157 // } else {
158 // return std::unique_ptr<Foo>(new Foo(arg));
159 // }
160 // }
161 //
162 template <typename T>
163 class RTCErrorOr {
164 // Used to convert between RTCErrorOr<Foo>/RtcErrorOr<Bar>, when an implicit
165 // conversion from Foo to Bar exists.
166 template <typename U>
167 friend class RTCErrorOr;
168
169 public:
170 typedef T element_type;
171
172 // Constructs a new RTCErrorOr with RTCErrorType::NONE error. This is marked
173 // 'explicit' to try to catch cases like 'return {};', where people think
174 // RTCErrorOr<std::vector<int>> will be initialized with an empty vector,
175 // instead of a RTCErrorType::NONE error.
176 explicit RTCErrorOr() = default;
177
178 // Constructs a new RTCErrorOr with the given non-ok error. After calling
179 // this constructor, calls to value() will DCHECK-fail.
180 //
181 // NOTE: Not explicit - we want to use RTCErrorOr<T> as a return
182 // value, so it is convenient and sensible to be able to do 'return
183 // RTCError(...)' when the return type is RTCErrorOr<T>.
184 //
185 // REQUIRES: !error.ok(). This requirement is DCHECKed.
186 RTCErrorOr(const RTCError& error) : error_(error) { RTC_DCHECK(!error.ok()); }
187 RTCErrorOr(RTCError&& error) : error_(std::move(error)) {
188 RTC_DCHECK(!error.ok());
189 }
190
191 // Constructs a new RTCErrorOr with the given value. After calling this
192 // constructor, calls to value() will succeed, and calls to error() will
193 // return a default-constructed RTCError.
194 //
195 // NOTE: Not explicit - we want to use RTCErrorOr<T> as a return type
196 // so it is convenient and sensible to be able to do 'return T()'
197 // when the return type is RTCErrorOr<T>.
198 RTCErrorOr(T value) : value_(std::move(value)) {}
199
200 // Delete the copy constructor and assignment operator; there aren't any use
201 // cases where you should need to copy an RTCErrorOr, as opposed to moving
202 // it. Can revisit this decision if use cases arise in the future.
203 RTCErrorOr(const RTCErrorOr& other) = delete;
204 RTCErrorOr& operator=(const RTCErrorOr& other) = delete;
205
206 // Move constructor and move-assignment operator.
207 RTCErrorOr(RTCErrorOr&& other) = default;
208 RTCErrorOr& operator=(RTCErrorOr&& other) = default;
209
210 // Conversion constructor and assignment operator; T must be copy or move
211 // constructible from U.
212 template <typename U>
213 RTCErrorOr(RTCErrorOr<U> other)
214 : error_(std::move(other.error_)), value_(std::move(other.value_)) {}
215 template <typename U>
216 RTCErrorOr& operator=(RTCErrorOr<U> other) {
217 error_ = std::move(other.error_);
218 value_ = std::move(other.value_);
219 return *this;
220 }
221
222 // Returns a reference to our error. If this contains a T, then returns
223 // default-constructed RTCError.
224 const RTCError& error() const { return error_; }
225
226 // Returns this->error().ok()
227 bool ok() const { return error_.ok(); }
228
229 // Returns a reference to our current value, or DCHECK-fails if !this->ok().
230 //
231 // Can be convenient for the implementation; for example, a method may want
232 // to access the value in some way before returning it to the next method on
233 // the stack.
234 const T& value() const {
235 RTC_DCHECK(ok());
236 return value_;
237 }
238 T& value() {
239 RTC_DCHECK(ok());
240 return value_;
241 }
242
243 // Moves our current value out of this object and returns it, or DCHECK-fails
244 // if !this->ok().
245 T MoveValue() {
246 RTC_DCHECK(ok());
247 return std::move(value_);
248 }
249
250 private:
251 RTCError error_;
tommi 2017/02/13 12:10:01 this also means that every return value that uses
Taylor Brandstetter 2017/02/13 18:03:53 I'd hoped that, by using move constructors/assignm
tommi 2017/02/13 18:17:14 I understand. The overhead can still be reduced fu
252 T value_;
253 };
254
255 } // namespace webrtc
256
257 #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