Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions errorcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,19 @@ var errorReasons = map[ErrorCode][]byte{
CodeAddrFamilyNotSupported: []byte("Address Family not Supported"),
CodePeerAddrFamilyMismatch: []byte("Peer Address Family Mismatch"),
}

// TurnError represents an error from a TURN response.
type TurnError struct {
StunMessageType MessageType
ErrorCodeAttr ErrorCodeAttribute
}

// Error returns the formatted TURN error message.
func (e TurnError) Error() string {
return fmt.Sprintf("%s (error %s)", e.StunMessageType, e.ErrorCodeAttr.String())
}

// String returns the error message as a string.
func (e TurnError) String() string {
return e.Error()
}
13 changes: 13 additions & 0 deletions errorcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,16 @@ func TestErrorCode(t *testing.T) {
attr.Reason = make([]byte, 2048)
assert.Error(t, attr.AddTo(m), "should error")
}

func TestTurnError(t *testing.T) {
te := TurnError{
StunMessageType: NewType(MethodCreatePermission, ClassErrorResponse),
ErrorCodeAttr: ErrorCodeAttribute{
Code: CodeForbidden,
Reason: []byte("Forbidden"),
},
}
expected := "CreatePermission error response (error 403: Forbidden)"
assert.Equal(t, expected, te.Error())
assert.Equal(t, expected, te.String())
}
Loading