From 3edec7cf1b342e8b76fbf14ba25351f7338c0d42 Mon Sep 17 00:00:00 2001 From: deadbeef Date: Sat, 10 Dec 2016 11:44:26 -0800 Subject: [PATCH] Adding error enum to be used by PeerConnectionInterface methods. The enum is at about the same level of detail as DOMExceptions, and I looked through the spec making sure that chromium will be able to perform the DOMException mapping for each one. The new enum is called RtcError and is outside the PeerConnectionInterface scope, because we may want to use this for things not associated with a PeerConnection in the future. This CL doesn't yet use the error enum anywhere; that will probably happen in follow-up CLs for the individual methods. BUG=webrtc:6855 Review-Url: https://codereview.webrtc.org/2564683002 Cr-Commit-Position: refs/heads/master@{#15526} --- webrtc/api/peerconnection.cc | 19 +++++++++ webrtc/api/peerconnectioninterface.h | 39 +++++++++++++++++++ .../api/peerconnectioninterface_unittest.cc | 9 +++++ 3 files changed, 67 insertions(+) diff --git a/webrtc/api/peerconnection.cc b/webrtc/api/peerconnection.cc index a47b2f2d8c..e1f19adf3e 100644 --- a/webrtc/api/peerconnection.cc +++ b/webrtc/api/peerconnection.cc @@ -432,6 +432,25 @@ void SetChannelOnSendersAndReceivers(CHANNEL* channel, namespace webrtc { +static const char* const kRtcErrorNames[] = { + "NONE", + "UNSUPPORTED_PARAMETER", + "INVALID_PARAMETER", + "INVALID_RANGE", + "SYNTAX_ERROR", + "INVALID_STATE", + "INVALID_MODIFICATION", + "NETWORK_ERROR", + "INTERNAL_ERROR", +}; + +std::ostream& operator<<(std::ostream& stream, RtcError error) { + int index = static_cast(error); + RTC_CHECK(index < static_cast(sizeof(kRtcErrorNames) / + sizeof(kRtcErrorNames[0]))); + return stream << kRtcErrorNames[index]; +} + // Generate a RTCP CNAME when a PeerConnection is created. std::string GenerateRtcpCname() { std::string cname; diff --git a/webrtc/api/peerconnectioninterface.h b/webrtc/api/peerconnectioninterface.h index 54fae2b711..a6557562a4 100644 --- a/webrtc/api/peerconnectioninterface.h +++ b/webrtc/api/peerconnectioninterface.h @@ -52,6 +52,7 @@ #define WEBRTC_API_PEERCONNECTIONINTERFACE_H_ #include +#include #include #include #include @@ -140,6 +141,44 @@ class MetricsObserverInterface : public rtc::RefCountInterface { typedef MetricsObserverInterface UMAObserver; +// Enumeration to represent distinct classes of errors that an application +// may wish to act upon differently. These roughly map to DOMExceptions in +// the web API, as described in the comments below. +enum class RtcError { + // No error. + NONE, + // A supplied parameter is valid, but currently unsupported. + // Maps to InvalidAccessError DOMException. + UNSUPPORTED_PARAMETER, + // General error indicating that a supplied parameter is invalid. + // Maps to InvalidAccessError or TypeError DOMException depending on context. + INVALID_PARAMETER, + // Slightly more specific than INVALID_PARAMETER; a parameter's value was + // outside the allowed range. + // Maps to RangeError DOMException. + INVALID_RANGE, + // Slightly more specific than INVALID_PARAMETER; an error occurred while + // parsing string input. + // Maps to SyntaxError DOMException. + SYNTAX_ERROR, + // The object does not support this operation in its current state. + // Maps to InvalidStateError DOMException. + INVALID_STATE, + // An attempt was made to modify the object in an invalid way. + // Maps to InvalidModificationError DOMException. + INVALID_MODIFICATION, + // An error occurred within an underlying network protocol. + // Maps to NetworkError DOMException. + NETWORK_ERROR, + // The operation failed due to an internal error. + // Maps to OperationError DOMException. + INTERNAL_ERROR, +}; + +// Outputs the error as a friendly string. +// Update this method when adding a new error type. +std::ostream& operator<<(std::ostream& stream, RtcError error); + class PeerConnectionInterface : public rtc::RefCountInterface { public: // See http://dev.w3.org/2011/webrtc/editor/webrtc.html#state-definitions . diff --git a/webrtc/api/peerconnectioninterface_unittest.cc b/webrtc/api/peerconnectioninterface_unittest.cc index d673b4174a..cfba3e67ec 100644 --- a/webrtc/api/peerconnectioninterface_unittest.cc +++ b/webrtc/api/peerconnectioninterface_unittest.cc @@ -9,6 +9,7 @@ */ #include +#include #include #include @@ -2989,3 +2990,11 @@ TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) { EXPECT_TRUE(updated_answer_options.has_audio()); EXPECT_TRUE(updated_answer_options.has_video()); } + +TEST(RtcErrorTest, OstreamOperator) { + std::ostringstream oss; + oss << webrtc::RtcError::NONE << ' ' + << webrtc::RtcError::INVALID_PARAMETER << ' ' + << webrtc::RtcError::INTERNAL_ERROR; + EXPECT_EQ("NONE INVALID_PARAMETER INTERNAL_ERROR", oss.str()); +}