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