Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 04:00 UTC
templates: Propagate httpError to HTTP response
Now possible with Go 1.17. See https://github.com/golang/go/issues/34201.
Matthew Holt committed
Sep 15, 2021 at 09:55 UTC
2392478bd3cf3e2878b3fa0eac8859a6374e0fa8
2 files changed
+9
-3
modules/caddyhttp/templates/templates.go
+7
@@ -16,6 +16,7 @@ package templates
16
17
import (
18
"bytes"
19
+ "errors"
20
"fmt"
21
"net/http"
22
"strconv"
@@ -312,6 +313,12 @@ func (t *Templates) executeTemplate(rr caddyhttp.ResponseRecorder, r *http.Reque
313
314
err := ctx.executeTemplateInBuffer(r.URL.Path, rr.Buffer())
315
if err != nil {
316
+ // templates may return a custom HTTP error to be propagated to the client,
317
+ // otherwise for any other error we assume the template is broken
318
+ var handlerErr caddyhttp.HandlerError
319
+ if errors.As(err, &handlerErr) {
320
+ return handlerErr
321
+ }
322
return caddyhttp.Error(http.StatusInternalServerError, err)
323
}
324
modules/caddyhttp/templates/tplcontext.go
+2
-3
@@ -350,9 +350,8 @@ func (c TemplateContext) funcFileExists(filename string) (bool, error) {
350
return false, nil
351
}
352
353
-// funcHTTPError returns a structured HTTP handler error. EXPERIMENTAL.
354
-// TODO: Requires https://github.com/golang/go/issues/34201 to be fixed (Go 1.17).
355
-// Example usage might be: `{{if not (fileExists $includeFile)}}{{httpError 404}}{{end}}`
353
+// funcHTTPError returns a structured HTTP handler error. EXPERIMENTAL; SUBJECT TO CHANGE.
354
+// Example usage: `{{if not (fileExists $includeFile)}}{{httpError 404}}{{end}}`
355
func (c TemplateContext) funcHTTPError(statusCode int) (bool, error) {
356
return false, caddyhttp.Error(statusCode, nil)
357
}