Added error wrapper (#252)

* Added error wrapper

* Keep the same message format
This commit is contained in:
Bassam Ajam
2025-10-25 05:41:39 -04:00
committed by GitHub
parent 67c546faa9
commit 23c631123f
2 changed files with 29 additions and 0 deletions

View File

@@ -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()
}

View File

@@ -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())
}