api: fix validation and http response

This commit is contained in:
yusing
2025-01-04 09:01:52 +08:00
parent 112859caa5
commit c30d3f585f
6 changed files with 33 additions and 16 deletions

View File

@@ -4,6 +4,7 @@ import (
"net/http"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/utils/strutils/ansi"
)
// HandleErr logs the error and returns an HTTP error response to the client.
@@ -23,6 +24,13 @@ func HandleErr(w http.ResponseWriter, r *http.Request, origErr error, code ...in
http.Error(w, http.StatusText(statusCode), statusCode)
}
func RespondError(w http.ResponseWriter, err error, code ...int) {
if len(code) > 0 {
w.WriteHeader(code[0])
}
WriteBody(w, []byte(ansi.StripANSI(err.Error())))
}
func ErrMissingKey(k string) error {
return E.New("missing key '" + k + "' in query or request body")
}