mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-14 14:56:27 +03:00
templates: Propagate httpError to HTTP response
Now possible with Go 1.17. See https://github.com/golang/go/issues/34201.
This commit is contained in:
parent
a437206643
commit
2392478bd3
2 changed files with 9 additions and 3 deletions
|
@ -16,6 +16,7 @@ package templates
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -312,6 +313,12 @@ func (t *Templates) executeTemplate(rr caddyhttp.ResponseRecorder, r *http.Reque
|
||||||
|
|
||||||
err := ctx.executeTemplateInBuffer(r.URL.Path, rr.Buffer())
|
err := ctx.executeTemplateInBuffer(r.URL.Path, rr.Buffer())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// templates may return a custom HTTP error to be propagated to the client,
|
||||||
|
// otherwise for any other error we assume the template is broken
|
||||||
|
var handlerErr caddyhttp.HandlerError
|
||||||
|
if errors.As(err, &handlerErr) {
|
||||||
|
return handlerErr
|
||||||
|
}
|
||||||
return caddyhttp.Error(http.StatusInternalServerError, err)
|
return caddyhttp.Error(http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -350,9 +350,8 @@ func (c TemplateContext) funcFileExists(filename string) (bool, error) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// funcHTTPError returns a structured HTTP handler error. EXPERIMENTAL.
|
// funcHTTPError returns a structured HTTP handler error. EXPERIMENTAL; SUBJECT TO CHANGE.
|
||||||
// TODO: Requires https://github.com/golang/go/issues/34201 to be fixed (Go 1.17).
|
// Example usage: `{{if not (fileExists $includeFile)}}{{httpError 404}}{{end}}`
|
||||||
// Example usage might be: `{{if not (fileExists $includeFile)}}{{httpError 404}}{{end}}`
|
|
||||||
func (c TemplateContext) funcHTTPError(statusCode int) (bool, error) {
|
func (c TemplateContext) funcHTTPError(statusCode int) (bool, error) {
|
||||||
return false, caddyhttp.Error(statusCode, nil)
|
return false, caddyhttp.Error(statusCode, nil)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue