Skip to content

Commit

Permalink
better parse errors if there is no text returned
Browse files Browse the repository at this point in the history
  • Loading branch information
firefart committed Dec 29, 2022
1 parent 2fa0a26 commit 98e7178
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions internal/types_stun.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"encoding/binary"
"fmt"
"strings"

"github.com/firefart/stunner/internal/helper"
)
Expand Down Expand Up @@ -300,11 +301,22 @@ type Error struct {

// ParseError returns an Error type from a byte slice
func ParseError(buf []byte) Error {
errorCode := int(buf[2])*100 + int(buf[3])
errorText := buf[4:]
errorCode := ErrorCode(int(buf[2])*100 + int(buf[3]))
errorText := string(buf[4:])
if len(strings.TrimSpace(errorText)) == 0 {
if tmp, ok := errorNames[errorCode]; ok {
errorText = tmp
} else if tmp, ok := TurnErrorNames[errorCode]; ok {
errorText = tmp
} else if tmp, ok := TurnTCPErrorNames[errorCode]; ok {
errorText = tmp
} else {
errorText = "Invalid Error"
}
}
return Error{
ErrorCode: ErrorCode(errorCode),
ErrorText: string(errorText),
ErrorCode: errorCode,
ErrorText: errorText,
}
}

Expand Down

0 comments on commit 98e7178

Please sign in to comment.