quorate/services/stalwart/cmd/controller/http_error.go

25 lines
628 B
Go
Raw Permalink Normal View History

package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func responseStatusError(action string, response *http.Response, secrets ...string) error {
body, err := io.ReadAll(io.LimitReader(response.Body, 4096))
if err != nil {
return fmt.Errorf("%s: HTTP %d", action, response.StatusCode)
}
detail := strings.TrimSpace(string(body))
for _, secret := range secrets {
if secret != "" {
detail = strings.ReplaceAll(detail, secret, "[redacted]")
}
}
if detail == "" {
return fmt.Errorf("%s: HTTP %d", action, response.StatusCode)
}
return fmt.Errorf("%s: HTTP %d: %q", action, response.StatusCode, detail)
}