From ed9f7faece31879d8d2692e39c8e7c6df0ab9439 Mon Sep 17 00:00:00 2001 From: Sanan Kornyakov <9613983333@mail.ru> Date: Tue, 25 Nov 2025 00:40:19 +0300 Subject: [PATCH] Add an error for sdp unmarshalling error Add the publicly available ErrSDPUnmarshal error when an sdp unmarshal fails. --- errors.go | 3 +++ sessiondescription.go | 7 ++++++- sessiondescription_test.go | 10 ++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/errors.go b/errors.go index d482920b..acece24d 100644 --- a/errors.go +++ b/errors.go @@ -175,6 +175,9 @@ var ( // and the requested SSRC was ignored. ErrSimulcastProbeOverflow = errors.New("simulcast probe limit has been reached, new SSRC has been discarded") + // ErrSDPUnmarshalling indicates that the SDP could not be unmarshalled. + ErrSDPUnmarshalling = errors.New("failed to unmarshal SDP") + errDetachNotEnabled = errors.New("enable detaching by calling webrtc.DetachDataChannels()") errDetachBeforeOpened = errors.New("datachannel not opened yet, try calling Detach from OnOpen") errDtlsTransportNotStarted = errors.New("the DTLS transport has not started yet") diff --git a/sessiondescription.go b/sessiondescription.go index 186f8583..7b6386ef 100644 --- a/sessiondescription.go +++ b/sessiondescription.go @@ -4,6 +4,8 @@ package webrtc import ( + "fmt" + "github.com/pion/sdp/v3" ) @@ -20,6 +22,9 @@ type SessionDescription struct { func (sd *SessionDescription) Unmarshal() (*sdp.SessionDescription, error) { sd.parsed = &sdp.SessionDescription{} err := sd.parsed.UnmarshalString(sd.SDP) + if err != nil { + return nil, fmt.Errorf("%w: %w", ErrSDPUnmarshalling, err) + } - return sd.parsed, err + return sd.parsed, nil } diff --git a/sessiondescription_test.go b/sessiondescription_test.go index 8214f99f..8057debd 100644 --- a/sessiondescription_test.go +++ b/sessiondescription_test.go @@ -85,3 +85,13 @@ func TestSessionDescription_Unmarshal(t *testing.T) { // check if the two parsed results _really_ match, could be affected by internal caching assert.True(t, reflect.DeepEqual(parsed1, parsed2)) } + +func TestSessionDescription_UnmarshalError(t *testing.T) { + desc := SessionDescription{ + Type: SDPTypeOffer, + SDP: "invalid sdp", + } + assert.Nil(t, desc.parsed) + _, err := desc.Unmarshal() + assert.ErrorIs(t, err, ErrSDPUnmarshalling) +}