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