Skip to content

Commit 23c6311

Browse files
Added error wrapper (#252)
* Added error wrapper * Keep the same message format
1 parent 67c546f commit 23c6311

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

errorcode.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,19 @@ var errorReasons = map[ErrorCode][]byte{
163163
CodeAddrFamilyNotSupported: []byte("Address Family not Supported"),
164164
CodePeerAddrFamilyMismatch: []byte("Peer Address Family Mismatch"),
165165
}
166+
167+
// TurnError represents an error from a TURN response.
168+
type TurnError struct {
169+
StunMessageType MessageType
170+
ErrorCodeAttr ErrorCodeAttribute
171+
}
172+
173+
// Error returns the formatted TURN error message.
174+
func (e TurnError) Error() string {
175+
return fmt.Sprintf("%s (error %s)", e.StunMessageType, e.ErrorCodeAttr.String())
176+
}
177+
178+
// String returns the error message as a string.
179+
func (e TurnError) String() string {
180+
return e.Error()
181+
}

errorcode_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,16 @@ func TestErrorCode(t *testing.T) {
8989
attr.Reason = make([]byte, 2048)
9090
assert.Error(t, attr.AddTo(m), "should error")
9191
}
92+
93+
func TestTurnError(t *testing.T) {
94+
te := TurnError{
95+
StunMessageType: NewType(MethodCreatePermission, ClassErrorResponse),
96+
ErrorCodeAttr: ErrorCodeAttribute{
97+
Code: CodeForbidden,
98+
Reason: []byte("Forbidden"),
99+
},
100+
}
101+
expected := "CreatePermission error response (error 403: Forbidden)"
102+
assert.Equal(t, expected, te.Error())
103+
assert.Equal(t, expected, te.String())
104+
}

0 commit comments

Comments
 (0)