feat(rules): support variables for error comand

This commit is contained in:
yusing
2025-10-26 20:25:46 +08:00
parent ee9bf31d30
commit 386d946bd2

View File

@@ -188,15 +188,20 @@ var commands = map[string]struct {
if !httputils.IsStatusCodeValid(code) {
return nil, ErrInvalidArguments.Subject(codeStr)
}
return &Tuple[int, string]{code, text}, nil
textTmpl, err := validateTemplate(text, true)
if err != nil {
return nil, ErrInvalidArguments.With(err)
}
return &Tuple[int, templateString]{code, textTmpl}, nil
},
build: func(args any) CommandHandler {
code, text := args.(*Tuple[int, string]).Unpack()
code, textTmpl := args.(*Tuple[int, templateString]).Unpack()
return TerminatingCommand(func(w http.ResponseWriter, r *http.Request) error {
// error command should overwrite the response body
GetInitResponseModifier(w).ResetBody()
http.Error(w, text, code)
return nil
w.WriteHeader(code)
err := textTmpl.ExpandVars(w, r, w)
return err
})
},
},