Skip to content

Commit 106f1ec

Browse files
committed
admin: fix warnings and status code in /load API
1 parent 39ace45 commit 106f1ec

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

caddyconfig/load.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,37 +91,42 @@ func (adminLoad) handleLoad(w http.ResponseWriter, r *http.Request) error {
9191
}
9292
body := buf.Bytes()
9393

94-
// if the config is formatted other than Caddy's native
95-
// JSON, we need to adapt it before loading it
94+
var warnings []Warning
9695
if ctHeader := r.Header.Get("Content-Type"); ctHeader != "" {
97-
result, warnings, err := adaptByContentType(ctHeader, body)
96+
result, wns, err := adaptByContentType(ctHeader, body)
9897
if err != nil {
9998
return caddy.APIError{
10099
HTTPStatus: http.StatusBadRequest,
101100
Err: err,
102101
}
103102
}
104-
if len(warnings) > 0 {
105-
respBody, err := json.Marshal(warnings)
106-
if err != nil {
107-
caddy.Log().Named("admin.api.load").Error(err.Error())
108-
}
109-
_, _ = w.Write(respBody)
110-
}
103+
warnings = wns
111104
body = result
112105
}
113106

114107
forceReload := r.Header.Get("Cache-Control") == "must-revalidate"
115108

116-
err = caddy.Load(body, forceReload)
117-
if err != nil {
118-
return caddy.APIError{
119-
HTTPStatus: http.StatusBadRequest,
120-
Err: fmt.Errorf("loading config: %v", err),
109+
if err := caddy.Load(body, forceReload); err != nil {
110+
errResp := struct {
111+
Error string `json:"error"`
112+
Warnings []Warning `json:"warnings,omitempty"`
113+
}{
114+
Error: fmt.Sprintf("loading config: %v", err),
115+
Warnings: warnings,
121116
}
117+
w.WriteHeader(http.StatusBadRequest)
118+
return json.NewEncoder(w).Encode(errResp)
122119
}
123120

124121
caddy.Log().Named("admin.api").Info("load complete")
122+
if len(warnings) > 0 {
123+
resp := struct {
124+
Warnings []Warning `json:"warnings,omitempty"`
125+
}{
126+
Warnings: warnings,
127+
}
128+
return json.NewEncoder(w).Encode(resp)
129+
}
125130

126131
return nil
127132
}

0 commit comments

Comments
 (0)