From 98e71781f1ccd06e0460e592506c41e68f27c636 Mon Sep 17 00:00:00 2001 From: firefart <105281+firefart@users.noreply.github.com> Date: Thu, 29 Dec 2022 09:42:33 +0100 Subject: [PATCH] better parse errors if there is no text returned --- internal/types_stun.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/internal/types_stun.go b/internal/types_stun.go index accad859..ab5185af 100644 --- a/internal/types_stun.go +++ b/internal/types_stun.go @@ -3,6 +3,7 @@ package internal import ( "encoding/binary" "fmt" + "strings" "github.com/firefart/stunner/internal/helper" ) @@ -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, } }