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

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

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

Powered by Google App Engine
This is Rietveld 408576698