123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- #ifndef API_RTC_ERROR_H_
- #define API_RTC_ERROR_H_
- #ifdef UNIT_TEST
- #include <ostream>
- #endif
- #include <string>
- #include <utility> // For std::move.
- #include "absl/types/optional.h"
- #include "rtc_base/checks.h"
- #include "rtc_base/logging.h"
- #include "rtc_base/system/rtc_export.h"
- namespace webrtc {
- enum class RTCErrorType {
-
- NONE,
-
-
- UNSUPPORTED_OPERATION,
-
-
- UNSUPPORTED_PARAMETER,
-
-
- INVALID_PARAMETER,
-
-
-
- INVALID_RANGE,
-
-
-
- SYNTAX_ERROR,
-
-
- INVALID_STATE,
-
-
- INVALID_MODIFICATION,
-
-
- NETWORK_ERROR,
-
-
-
- RESOURCE_EXHAUSTED,
-
-
- INTERNAL_ERROR,
-
-
-
-
- OPERATION_ERROR_WITH_DATA,
- };
- enum class RTCErrorDetailType {
- NONE,
- DATA_CHANNEL_FAILURE,
- DTLS_FAILURE,
- FINGERPRINT_FAILURE,
- SCTP_FAILURE,
- SDP_SYNTAX_ERROR,
- HARDWARE_ENCODER_NOT_AVAILABLE,
- HARDWARE_ENCODER_ERROR,
- };
- class RTC_EXPORT RTCError {
- public:
-
-
- RTCError() {}
- explicit RTCError(RTCErrorType type) : type_(type) {}
- RTCError(RTCErrorType type, std::string message)
- : type_(type), message_(std::move(message)) {}
-
-
-
- RTCError(const RTCError& other) = default;
- RTCError(RTCError&&) = default;
- RTCError& operator=(const RTCError& other) = default;
- RTCError& operator=(RTCError&&) = default;
-
-
-
- static RTCError OK();
-
- RTCErrorType type() const { return type_; }
- void set_type(RTCErrorType type) { type_ = type; }
-
-
-
- const char* message() const;
- void set_message(std::string message);
- RTCErrorDetailType error_detail() const { return error_detail_; }
- void set_error_detail(RTCErrorDetailType detail) { error_detail_ = detail; }
- absl::optional<uint16_t> sctp_cause_code() const { return sctp_cause_code_; }
- void set_sctp_cause_code(uint16_t cause_code) {
- sctp_cause_code_ = cause_code;
- }
-
-
- bool ok() const { return type_ == RTCErrorType::NONE; }
- private:
- RTCErrorType type_ = RTCErrorType::NONE;
- std::string message_;
- RTCErrorDetailType error_detail_ = RTCErrorDetailType::NONE;
- absl::optional<uint16_t> sctp_cause_code_;
- };
- RTC_EXPORT const char* ToString(RTCErrorType error);
- RTC_EXPORT const char* ToString(RTCErrorDetailType error);
- #ifdef UNIT_TEST
- inline std::ostream& operator<<(
- std::ostream& stream,
- RTCErrorType error) {
- return stream << ToString(error);
- }
- inline std::ostream& operator<<(
- std::ostream& stream,
- RTCErrorDetailType error) {
- return stream << ToString(error);
- }
- #endif
- #define LOG_AND_RETURN_ERROR_EX(type, message, severity) \
- { \
- RTC_DCHECK(type != RTCErrorType::NONE); \
- RTC_LOG(severity) << message << " (" << ToString(type) << ")"; \
- return webrtc::RTCError(type, message); \
- }
- #define LOG_AND_RETURN_ERROR(type, message) \
- LOG_AND_RETURN_ERROR_EX(type, message, LS_ERROR)
- template <typename T>
- class RTCErrorOr {
-
-
- template <typename U>
- friend class RTCErrorOr;
- public:
- typedef T element_type;
-
-
-
-
- RTCErrorOr() : error_(RTCErrorType::INTERNAL_ERROR) {}
-
-
-
-
-
-
-
-
- RTCErrorOr(RTCError&& error) : error_(std::move(error)) {
- RTC_DCHECK(!error.ok());
- }
-
-
-
-
-
-
-
- RTCErrorOr(const T& value) : value_(value) {}
- RTCErrorOr(T&& value) : value_(std::move(value)) {}
-
-
-
- RTCErrorOr(const RTCErrorOr& other) = delete;
- RTCErrorOr& operator=(const RTCErrorOr& other) = delete;
-
-
-
-
-
- RTCErrorOr(RTCErrorOr&& other)
- : error_(std::move(other.error_)), value_(std::move(other.value_)) {}
- RTCErrorOr& operator=(RTCErrorOr&& other) {
- error_ = std::move(other.error_);
- value_ = std::move(other.value_);
- return *this;
- }
-
-
- template <typename U>
- RTCErrorOr(RTCErrorOr<U> other)
- : error_(std::move(other.error_)), value_(std::move(other.value_)) {}
- template <typename U>
- RTCErrorOr& operator=(RTCErrorOr<U> other) {
- error_ = std::move(other.error_);
- value_ = std::move(other.value_);
- return *this;
- }
-
-
- const RTCError& error() const { return error_; }
-
-
-
- RTCError MoveError() { return std::move(error_); }
-
- bool ok() const { return error_.ok(); }
-
-
-
-
-
- const T& value() const {
- RTC_DCHECK(ok());
- return value_;
- }
- T& value() {
- RTC_DCHECK(ok());
- return value_;
- }
-
-
- T MoveValue() {
- RTC_DCHECK(ok());
- return std::move(value_);
- }
- private:
- RTCError error_;
- T value_;
- };
- }
- #endif
|