blob: 4910682dbf3d30a604a9fb43ae706869fc6ffe63 [file] [log] [blame]
deadbeef6038e972017-02-17 07:31:331/*
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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef API_RTCERROR_H_
12#define API_RTCERROR_H_
deadbeef6038e972017-02-17 07:31:3313
Jonas Olsson3e18c822018-04-18 08:11:0714#ifdef UNIT_TEST
deadbeef6038e972017-02-17 07:31:3315#include <ostream>
Jonas Olsson3e18c822018-04-18 08:11:0716#endif // UNIT_TEST
deadbeef6038e972017-02-17 07:31:3317#include <string>
18#include <utility> // For std::move.
19
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
deadbeef6038e972017-02-17 07:31:3322
23namespace webrtc {
24
25// Enumeration to represent distinct classes of errors that an application
26// may wish to act upon differently. These roughly map to DOMExceptions or
27// RTCError "errorDetailEnum" values in the web API, as described in the
28// comments below.
29enum class RTCErrorType {
30 // No error.
31 NONE,
32
33 // An operation is valid, but currently unsupported.
34 // Maps to OperationError DOMException.
35 UNSUPPORTED_OPERATION,
36
37 // A supplied parameter is valid, but currently unsupported.
38 // Maps to OperationError DOMException.
39 UNSUPPORTED_PARAMETER,
40
41 // General error indicating that a supplied parameter is invalid.
42 // Maps to InvalidAccessError or TypeError DOMException depending on context.
43 INVALID_PARAMETER,
44
45 // Slightly more specific than INVALID_PARAMETER; a parameter's value was
46 // outside the allowed range.
47 // Maps to RangeError DOMException.
48 INVALID_RANGE,
49
50 // Slightly more specific than INVALID_PARAMETER; an error occurred while
51 // parsing string input.
52 // Maps to SyntaxError DOMException.
53 SYNTAX_ERROR,
54
55 // The object does not support this operation in its current state.
56 // Maps to InvalidStateError DOMException.
57 INVALID_STATE,
58
59 // An attempt was made to modify the object in an invalid way.
60 // Maps to InvalidModificationError DOMException.
61 INVALID_MODIFICATION,
62
63 // An error occurred within an underlying network protocol.
64 // Maps to NetworkError DOMException.
65 NETWORK_ERROR,
66
67 // Some resource has been exhausted; file handles, hardware resources, ports,
68 // etc.
69 // Maps to OperationError DOMException.
70 RESOURCE_EXHAUSTED,
71
72 // The operation failed due to an internal error.
73 // Maps to OperationError DOMException.
74 INTERNAL_ERROR,
75};
76
77// Roughly corresponds to RTCError in the web api. Holds an error type, a
78// message, and possibly additional information specific to that error.
79//
80// Doesn't contain anything beyond a type and message now, but will in the
81// future as more errors are implemented.
82class RTCError {
83 public:
84 // Constructors.
85
86 // Creates a "no error" error.
87 RTCError() {}
88 explicit RTCError(RTCErrorType type) : type_(type) {}
Jonas Olsson941a07c2018-09-13 08:07:0789
90 RTCError(RTCErrorType type, std::string message)
91 : type_(type), message_(std::move(message)) {}
deadbeef6038e972017-02-17 07:31:3392
93 // Delete the copy constructor and assignment operator; there aren't any use
94 // cases where you should need to copy an RTCError, as opposed to moving it.
95 // Can revisit this decision if use cases arise in the future.
96 RTCError(const RTCError& other) = delete;
97 RTCError& operator=(const RTCError& other) = delete;
98
99 // Move constructor and move-assignment operator.
100 RTCError(RTCError&& other);
101 RTCError& operator=(RTCError&& other);
102
deadbeef6038e972017-02-17 07:31:33103 // Identical to default constructed error.
104 //
105 // Preferred over the default constructor for code readability.
106 static RTCError OK();
107
108 // Error type.
109 RTCErrorType type() const { return type_; }
110 void set_type(RTCErrorType type) { type_ = type; }
111
112 // Human-readable message describing the error. Shouldn't be used for
113 // anything but logging/diagnostics, since messages are not guaranteed to be
114 // stable.
115 const char* message() const;
Jonas Olsson941a07c2018-09-13 08:07:07116
117 void set_message(std::string message);
deadbeef6038e972017-02-17 07:31:33118
119 // Convenience method for situations where you only care whether or not an
120 // error occurred.
121 bool ok() const { return type_ == RTCErrorType::NONE; }
122
123 private:
124 RTCErrorType type_ = RTCErrorType::NONE;
Jonas Olsson941a07c2018-09-13 08:07:07125 std::string message_;
deadbeef6038e972017-02-17 07:31:33126};
127
128// Outputs the error as a friendly string. Update this method when adding a new
129// error type.
130//
131// Only intended to be used for logging/disagnostics.
Taylor Brandstetterbd739282018-04-20 15:58:11132std::string ToString(RTCErrorType error);
Jonas Olssond7ee7202018-04-18 08:11:07133
Jonas Olsson3e18c822018-04-18 08:11:07134#ifdef UNIT_TEST
135inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
136 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
137 RTCErrorType error) {
138 return stream << ToString(error);
139}
140#endif // UNIT_TEST
141
deadbeef6038e972017-02-17 07:31:33142// Helper macro that can be used by implementations to create an error with a
143// message and log it. |message| should be a string literal or movable
144// std::string.
Jonas Olssonabbe8412018-04-03 11:40:05145#define LOG_AND_RETURN_ERROR_EX(type, message, severity) \
146 { \
147 RTC_DCHECK(type != RTCErrorType::NONE); \
148 RTC_LOG(severity) << message << " (" << ToString(type) << ")"; \
149 return webrtc::RTCError(type, message); \
deadbeef6038e972017-02-17 07:31:33150 }
151
152#define LOG_AND_RETURN_ERROR(type, message) \
153 LOG_AND_RETURN_ERROR_EX(type, message, LS_ERROR)
154
155// RTCErrorOr<T> is the union of an RTCError object and a T object. RTCErrorOr
156// models the concept of an object that is either a usable value, or an error
157// Status explaining why such a value is not present. To this end RTCErrorOr<T>
158// does not allow its RTCErrorType value to be RTCErrorType::NONE. This is
159// enforced by a debug check in most cases.
160//
161// The primary use-case for RTCErrorOr<T> is as the return value of a function
162// which may fail. For example, CreateRtpSender will fail if the parameters
163// could not be successfully applied at the media engine level, but if
164// successful will return a unique_ptr to an RtpSender.
165//
166// Example client usage for a RTCErrorOr<std::unique_ptr<T>>:
167//
168// RTCErrorOr<std::unique_ptr<Foo>> result = FooFactory::MakeNewFoo(arg);
169// if (result.ok()) {
170// std::unique_ptr<Foo> foo = result.ConsumeValue();
171// foo->DoSomethingCool();
172// } else {
Mirko Bonadei675513b2017-11-09 10:09:25173// RTC_LOG(LS_ERROR) << result.error();
deadbeef6038e972017-02-17 07:31:33174// }
175//
176// Example factory implementation returning RTCErrorOr<std::unique_ptr<T>>:
177//
178// RTCErrorOr<std::unique_ptr<Foo>> FooFactory::MakeNewFoo(int arg) {
179// if (arg <= 0) {
180// return RTCError(RTCErrorType::INVALID_RANGE, "Arg must be positive");
181// } else {
182// return std::unique_ptr<Foo>(new Foo(arg));
183// }
184// }
185//
186template <typename T>
187class RTCErrorOr {
188 // Used to convert between RTCErrorOr<Foo>/RtcErrorOr<Bar>, when an implicit
189 // conversion from Foo to Bar exists.
190 template <typename U>
191 friend class RTCErrorOr;
192
193 public:
194 typedef T element_type;
195
196 // Constructs a new RTCErrorOr with RTCErrorType::INTERNAL_ERROR error. This
197 // is marked 'explicit' to try to catch cases like 'return {};', where people
198 // think RTCErrorOr<std::vector<int>> will be initialized with an empty
199 // vector, instead of a RTCErrorType::INTERNAL_ERROR error.
oprypin8e58d652017-03-21 14:52:41200 RTCErrorOr() : error_(RTCErrorType::INTERNAL_ERROR) {}
deadbeef6038e972017-02-17 07:31:33201
202 // Constructs a new RTCErrorOr with the given non-ok error. After calling
203 // this constructor, calls to value() will DCHECK-fail.
204 //
205 // NOTE: Not explicit - we want to use RTCErrorOr<T> as a return
206 // value, so it is convenient and sensible to be able to do 'return
207 // RTCError(...)' when the return type is RTCErrorOr<T>.
208 //
209 // REQUIRES: !error.ok(). This requirement is DCHECKed.
oprypin8e58d652017-03-21 14:52:41210 RTCErrorOr(RTCError&& error) : error_(std::move(error)) { // NOLINT
deadbeef6038e972017-02-17 07:31:33211 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>.
oprypin8e58d652017-03-21 14:52:41221 RTCErrorOr(T&& value) : value_(std::move(value)) {} // NOLINT
deadbeef6038e972017-02-17 07:31:33222
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.
deadbeefb5388d72017-02-24 09:17:43230 //
231 // Visual Studio doesn't support "= default" with move constructors or
232 // assignment operators (even though they compile, they segfault), so define
233 // them explicitly.
234 RTCErrorOr(RTCErrorOr&& other)
235 : error_(std::move(other.error_)), value_(std::move(other.value_)) {}
236 RTCErrorOr& operator=(RTCErrorOr&& other) {
237 error_ = std::move(other.error_);
238 value_ = std::move(other.value_);
239 return *this;
240 }
deadbeef6038e972017-02-17 07:31:33241
242 // Conversion constructor and assignment operator; T must be copy or move
243 // constructible from U.
244 template <typename U>
oprypin8e58d652017-03-21 14:52:41245 RTCErrorOr(RTCErrorOr<U> other) // NOLINT
deadbeef6038e972017-02-17 07:31:33246 : error_(std::move(other.error_)), value_(std::move(other.value_)) {}
247 template <typename U>
248 RTCErrorOr& operator=(RTCErrorOr<U> other) {
249 error_ = std::move(other.error_);
250 value_ = std::move(other.value_);
251 return *this;
252 }
253
254 // Returns a reference to our error. If this contains a T, then returns
255 // default-constructed RTCError.
256 const RTCError& error() const { return error_; }
257
258 // Moves the error. Can be useful if, say "CreateFoo" returns an
259 // RTCErrorOr<Foo>, and internally calls "CreateBar" which returns an
260 // RTCErrorOr<Bar>, and wants to forward the error up the stack.
261 RTCError MoveError() { return std::move(error_); }
262
263 // Returns this->error().ok()
264 bool ok() const { return error_.ok(); }
265
266 // Returns a reference to our current value, or DCHECK-fails if !this->ok().
267 //
268 // Can be convenient for the implementation; for example, a method may want
269 // to access the value in some way before returning it to the next method on
270 // the stack.
271 const T& value() const {
272 RTC_DCHECK(ok());
273 return value_;
274 }
275 T& value() {
276 RTC_DCHECK(ok());
277 return value_;
278 }
279
280 // Moves our current value out of this object and returns it, or DCHECK-fails
281 // if !this->ok().
282 T MoveValue() {
283 RTC_DCHECK(ok());
284 return std::move(value_);
285 }
286
287 private:
288 RTCError error_;
289 T value_;
290};
291
292} // namespace webrtc
293
Mirko Bonadei92ea95e2017-09-15 04:47:31294#endif // API_RTCERROR_H_