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 | |
84 // Creates a "no error" error. | |
85 RTCError() = default; | |
86 explicit RTCError(RTCErrorType type) : type_(type) {} | |
87 RTCError(RTCErrorType type, std::string message) | |
tommi
2017/02/12 12:11:22
is |message| expected to be std::move()d? also, u
Taylor Brandstetter
2017/02/12 16:19:23
Just meant to use a const ref, fixed.
| |
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(); | |
Taylor Brandstetter
2017/02/12 02:36:37
This is the only thing other than RTCErrorOr and C
| |
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 std::string message() const { return message_; } | |
tommi
2017/02/12 12:11:21
return const&?
Taylor Brandstetter
2017/02/12 16:19:23
Done.
| |
104 void set_message(const std::string& message) { message_ = message; } | |
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_; | |
113 }; | |
114 | |
115 // Outputs the error as a friendly string. | |
116 // Update this method when adding a new error type. | |
117 std::ostream& operator<<(std::ostream& stream, RTCErrorType error); | |
tommi
2017/02/12 12:11:22
Streams are costly in terms of code size and cpu u
Taylor Brandstetter
2017/02/12 16:19:23
Yes, it's only intended for logging. I'll add a co
| |
118 | |
119 // Helper method that can be used by implementations to create an error with a | |
120 // message and log it. | |
121 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type, | |
122 const std::string& message, | |
123 rtc::LoggingSeverity severity); | |
Taylor Brandstetter
2017/02/12 02:36:37
Can be used like:
return CreateAndLogError(RTCErr
| |
124 | |
125 // Logs at error level. | |
126 webrtc::RTCError CreateAndLogError(webrtc::RTCErrorType type, | |
127 const std::string& message); | |
128 | |
129 // RTCErrorOr<T> is the union of an RTCError object and a T object. RTCErrorOr | |
130 // models the concept of an object that is either a usable value, or an error | |
131 // Status explaining why such a value is not present. To this end RTCErrorOr<T> | |
132 // does not allow its RTCErrorType value to be RTCErrorType::NONE. This is | |
133 // enforced by a debug check in most cases. | |
134 // | |
135 // The primary use-case for RTCErrorOr<T> is as the return value of a function | |
136 // which may fail. For example, CreateRtpSender will fail if the parameters | |
137 // could not be successfully applied at the media engine level, but if | |
138 // successful will return a unique_ptr to an RtpSender. | |
139 // | |
140 // Example client usage for a RTCErrorOr<std::unique_ptr<T>>: | |
141 // | |
142 // RTCErrorOr<std::unique_ptr<Foo>> result = FooFactory::MakeNewFoo(arg); | |
143 // if (result.ok()) { | |
144 // std::unique_ptr<Foo> foo = result.ConsumeValue(); | |
145 // foo->DoSomethingCool(); | |
146 // } else { | |
147 // LOG(LS_ERROR) << result.error(); | |
148 // } | |
149 // | |
150 // Example factory implementation returning RTCErrorOr<std::unique_ptr<T>>: | |
151 // | |
152 // RTCErrorOr<std::unique_ptr<Foo>> FooFactory::MakeNewFoo(int arg) { | |
153 // if (arg <= 0) { | |
154 // return RTCError(RTCErrorType::INVALID_RANGE, "Arg must be positive"); | |
155 // } else { | |
156 // return std::unique_ptr<Foo>(new Foo(arg)); | |
157 // } | |
158 // } | |
159 // | |
160 template <typename T> | |
161 class RTCErrorOr { | |
162 // Used to convert between RTCErrorOr<Foo>/RtcErrorOr<Bar>, when an implicit | |
163 // conversion from Foo to Bar exists. | |
164 template <typename U> | |
165 friend class RTCErrorOr; | |
166 | |
167 public: | |
168 typedef T element_type; | |
169 | |
170 // Constructs a new RTCErrorOr with RTCErrorType::NONE error. This is marked | |
171 // 'explicit' to try to catch cases like 'return {};', where people think | |
172 // RTCErrorOr<std::vector<int>> will be initialized with an empty vector, | |
173 // instead of a RTCErrorType::NONE error. | |
174 explicit RTCErrorOr() = default; | |
175 | |
176 // Constructs a new RTCErrorOr with the given non-ok error. After calling | |
177 // this constructor, calls to value() will DCHECK-fail. | |
178 // | |
179 // NOTE: Not explicit - we want to use RTCErrorOr<T> as a return | |
180 // value, so it is convenient and sensible to be able to do 'return | |
181 // RTCError(...)' when the return type is RTCErrorOr<T>. | |
182 // | |
183 // REQUIRES: !error.ok(). This requirement is DCHECKed. | |
184 RTCErrorOr(const RTCError& error) : error_(error) { RTC_DCHECK(!error.ok()); } | |
tommi
2017/02/12 12:11:21
could this be |RTCError&& error| and move ownershi
Taylor Brandstetter
2017/02/12 16:19:23
Done.
| |
185 | |
186 // Constructs a new RTCErrorOr with the given value. After calling this | |
187 // constructor, calls to value() will succeed, and calls to error() will | |
188 // return a default-constructed RTCError. | |
189 // | |
190 // NOTE: Not explicit - we want to use RTCErrorOr<T> as a return type | |
191 // so it is convenient and sensible to be able to do 'return T()' | |
192 // when the return type is RTCErrorOr<T>. | |
193 RTCErrorOr(T value) : value_(std::move(value)) {} | |
194 | |
195 // Copy constructor. | |
tommi
2017/02/12 12:11:21
do we need one? (comment?)
if we could instead liv
Taylor Brandstetter
2017/02/12 16:19:23
I can't think of where it would be useful. I'll re
| |
196 RTCErrorOr(const RTCErrorOr& other) = default; | |
197 | |
198 // Assignment operator. | |
199 RTCErrorOr& operator=(const RTCErrorOr& other) = default; | |
200 | |
201 // Move constructor and move-assignment operator. | |
202 RTCErrorOr(RTCErrorOr&& other) = default; | |
203 RTCErrorOr& operator=(RTCErrorOr&& other) = default; | |
204 | |
205 // Conversion constructor and assignment operator; T must be copy or move | |
206 // constructible from U. | |
207 template <typename U> | |
208 RTCErrorOr(RTCErrorOr<U> other) | |
209 : error_(std::move(other.error_)), value_(std::move(other.value_)) {} | |
210 template <typename U> | |
211 RTCErrorOr& operator=(RTCErrorOr<U> other) { | |
212 error_ = std::move(other.error_); | |
213 value_ = std::move(other.value_); | |
214 return *this; | |
215 } | |
216 | |
217 // Returns a reference to our error. If this contains a T, then returns | |
218 // default-constructed RTCError. | |
219 const RTCError& error() const { return error_; } | |
220 | |
221 // Returns this->error().ok() | |
222 bool ok() const { return error_.ok(); } | |
223 | |
224 // Returns a reference to our current value, or DCHECK-fails if !this->ok(). | |
225 const T& value() const { | |
226 RTC_DCHECK(ok()); | |
227 return value_; | |
228 } | |
229 T& value() { | |
tommi
2017/02/12 12:11:21
can you add a comment that explains when this is n
Taylor Brandstetter
2017/02/12 16:19:23
It could be useful if the implementation, say, wan
tommi
2017/02/12 16:27:28
Would the |const T& value()| accessor cover that c
Taylor Brandstetter
2017/02/12 16:44:12
There are still some cases where the non-const-ref
tommi
2017/02/12 18:19:32
OK, if it's necessary, I'm not against it. What I
| |
230 RTC_DCHECK(ok()); | |
231 return value_; | |
232 } | |
233 | |
234 // Moves our current value out of this object and returns it, or DCHECK-fails | |
235 // if !this->ok(). | |
236 T MoveValue() { | |
237 RTC_DCHECK(ok()); | |
238 return std::move(value_); | |
239 } | |
240 | |
241 private: | |
242 RTCError error_; | |
243 T value_; | |
kwiberg-webrtc
2017/02/14 09:51:49
Hmm. So an RTCErrorOr always contains a T. This ma
Taylor Brandstetter
2017/02/15 02:22:23
I plan to use this for things that are either smal
| |
244 }; | |
245 | |
246 } // namespace webrtc | |
247 | |
248 #endif // WEBRTC_API_RTCERROR_H_ | |
OLD | NEW |