improved implementation of converting ANSI color to HTML

This commit is contained in:
yusing
2025-01-26 14:46:43 +08:00
parent a9da7ce6fc
commit 7ec42dce4d
3 changed files with 135 additions and 99 deletions

View File

@@ -4,6 +4,7 @@ import (
"net/http"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/utils/strutils/ansi"
)
@@ -30,8 +31,16 @@ func RespondError(w http.ResponseWriter, err error, code ...int) {
if len(code) == 0 {
code = []int{http.StatusBadRequest}
}
// strip ANSI color codes added from Error.WithSubject
http.Error(w, ansi.StripANSI(err.Error()), code[0])
w.Header().Set("Content-Type", "text/html; charset=utf-8")
buf := make([]byte, 0, 100)
errMsg := err.Error()
buf, err = logging.FormatMessageToHTMLBytes(errMsg, buf)
if err != nil {
http.Error(w, ansi.StripANSI(errMsg), code[0])
return
}
w.WriteHeader(code[0])
_, _ = w.Write(buf)
}
func ErrMissingKey(k string) error {