diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2021-09-15 09:55:57 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2021-09-15 09:55:57 -0600 |
commit | 2392478bd3cf3e2878b3fa0eac8859a6374e0fa8 (patch) | |
tree | bb16ac93a6460e3175cb8c205b9ee73bbdd0bb85 /modules/caddyhttp/templates/templates.go | |
parent | a437206643f35a84dea280c99e7e9555fc8df697 (diff) |
templates: Propagate httpError to HTTP response
Now possible with Go 1.17.
See https://github.com/golang/go/issues/34201.
Diffstat (limited to 'modules/caddyhttp/templates/templates.go')
-rw-r--r-- | modules/caddyhttp/templates/templates.go | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/modules/caddyhttp/templates/templates.go b/modules/caddyhttp/templates/templates.go index 33e5a70..6fed8c8 100644 --- a/modules/caddyhttp/templates/templates.go +++ b/modules/caddyhttp/templates/templates.go @@ -16,6 +16,7 @@ package templates import ( "bytes" + "errors" "fmt" "net/http" "strconv" @@ -312,6 +313,12 @@ func (t *Templates) executeTemplate(rr caddyhttp.ResponseRecorder, r *http.Reque err := ctx.executeTemplateInBuffer(r.URL.Path, rr.Buffer()) 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) } |