1//go:build go1.21
2
3// From go1.21 and onwards.
4
5package tlsrpt
6
7import (
8 "crypto/tls"
9 "fmt"
10 "strings"
11)
12
13// FormatAlert formats a TLS alert in the form "alert-<num>" or "alert-<num>-<shortcode>".
14func FormatAlert(alert uint8) string {
15 s := fmt.Sprintf("alert-%d", alert)
16 err := tls.AlertError(alert) // Since go1.21.0
17 // crypto/tls returns messages like "tls: short message" or "tls: alert(321)".
18 if str := err.Error(); !strings.Contains(str, "alert(") {
19 s += "-" + strings.ReplaceAll(strings.TrimPrefix(str, "tls: "), " ", "-")
20 }
21 return s
22}
23