attr: add default error reasons

This commit is contained in:
Aleksandr Razumov
2016-05-08 01:14:18 +03:00
parent 55ce3c0071
commit 6905ed655c
2 changed files with 41 additions and 0 deletions

34
attribute_errorcode.go Normal file
View 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"
}
}

View File

@@ -140,6 +140,13 @@ func (m *Message) AddErrorCode(code int, reason string) {
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.
func (m *Message) GetErrorCode() (int, []byte, error) {
v := m.getAttrValue(AttrErrorCode)