mirror of
https://github.com/gortc/stun.git
synced 2025-10-24 17:00:20 +08:00
attr: add default error reasons
This commit is contained in:
34
attribute_errorcode.go
Normal file
34
attribute_errorcode.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package stun
|
||||||
|
|
||||||
|
// ErrorCode is code for ERROR-CODE attribute.
|
||||||
|
type ErrorCode int
|
||||||
|
|
||||||
|
// Possible error codes.
|
||||||
|
const (
|
||||||
|
CodeTryAlternate ErrorCode = 300
|
||||||
|
CodeBadRequest ErrorCode = 400
|
||||||
|
CodeUnauthorised ErrorCode = 401
|
||||||
|
CodeUnknownAttribute ErrorCode = 420
|
||||||
|
CodeStaleNonce ErrorCode = 428
|
||||||
|
CodeServerError ErrorCode = 500
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reason returns recommended reason string.
|
||||||
|
func (c ErrorCode) Reason() string {
|
||||||
|
switch c {
|
||||||
|
case CodeTryAlternate:
|
||||||
|
return "Try Alternate"
|
||||||
|
case CodeBadRequest:
|
||||||
|
return "Bad Request"
|
||||||
|
case CodeUnauthorised:
|
||||||
|
return "Unauthorised"
|
||||||
|
case CodeUnknownAttribute:
|
||||||
|
return "Unknown attribute"
|
||||||
|
case CodeStaleNonce:
|
||||||
|
return "Stale Nonce"
|
||||||
|
case CodeServerError:
|
||||||
|
return "Server Error"
|
||||||
|
default:
|
||||||
|
return "Unknown Error"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -140,6 +140,13 @@ func (m *Message) AddErrorCode(code int, reason string) {
|
|||||||
m.Add(AttrErrorCode, value)
|
m.Add(AttrErrorCode, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddErrorCodeDefault is wrapper for AddErrorCode that uses recommended
|
||||||
|
// reason string from RFC. If error code is unknown, reason will be "Unknown
|
||||||
|
// Error".
|
||||||
|
func (m *Message) AddErrorCodeDefault(code int) {
|
||||||
|
m.AddErrorCode(code, ErrorCode(code).Reason())
|
||||||
|
}
|
||||||
|
|
||||||
// GetErrorCode returns ERROR-CODE code, reason and decode error if any.
|
// GetErrorCode returns ERROR-CODE code, reason and decode error if any.
|
||||||
func (m *Message) GetErrorCode() (int, []byte, error) {
|
func (m *Message) GetErrorCode() (int, []byte, error) {
|
||||||
v := m.getAttrValue(AttrErrorCode)
|
v := m.getAttrValue(AttrErrorCode)
|
||||||
|
|||||||
Reference in New Issue
Block a user