Templates in error pages

1. Output of caddy version:

v2.6.1

2. How I run Caddy:

a. System environment:

Arch Linux, systemd

b. Command:

caddy run

c. Service/unit/compose file:

d. My complete Caddy config:

localhost

root * projects/test/dist

file_server
encode gzip

try_files {path}.html {path}
try_files {path} {path}/ =404

handle_errors {
	file_server
	templates
	rewrite error
}

3. The problem I’m having:

In the docs, in handle_error Examples section it says:

A single error page that uses templates to write a custom error message:

handle_errors {
	rewrite * /error.html
	templates
	file_server
}

But there is nothing said about what should be put in the html, I tried using {{err.status_code}} {{err.status_text}} but that didn’t work.

4. Error messages and/or full log output:

2022/10/23 20:40:40.344	ERROR	http.log.error	error handling handler error	{"request": {"remote_ip": "127.0.0.1", "remote_port": "47560", "proto": "HTTP/2.0", "method": "GET", "host": "localhost", "uri": "/favicon.ico", "headers": {"User-Agent": ["Mozilla/5.0 (X11; Linux x86_64; rv:106.0) Gecko/20100101 Firefox/106.0"], "Accept": ["image/avif,image/webp,*/*"], "Accept-Language": ["en-US,en;q=0.5"], "Dnt": ["1"], "Referer": ["https://localhost/favicon.ico"], "Sec-Fetch-Dest": ["image"], "Cache-Control": ["no-cache"], "Accept-Encoding": ["gzip, deflate, br"], "Sec-Fetch-Mode": ["no-cors"], "Sec-Fetch-Site": ["same-origin"], "Sec-Gpc": ["1"], "Pragma": ["no-cache"], "Te": ["trailers"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "server_name": "localhost"}}, "duration": 0.000096811, "error": "{id=a86n20jvi} templates.(*Templates).executeTemplate (templates.go:403): HTTP 500: template: error:12: function \"err\" not defined", "first_error": {"msg": "404", "status": 404, "err_id": "ag6e5qr23", "err_trace": "fileserver.parseErrorCode (matcher.go:489)"}}

5. What I already tried:

Finding the documentation for templates,

{{err.status_code}} {{err.status_text}}

6. Links to relevant resources:

This doesn’t make sense. You shouldn’t have two try_files directives, they’ll step on eachother’s toes. Just define it once, with all the rewrite rules you need.

Are you trying to rewrite to a file named error? Probably best if you name it error.html instead, so that the correct mime type is detected (as per the example).

Unfortunately, the templates documentation on the website is broken right now :grimacing: It should appear here Modules - Caddy Documentation which is linked to from the top of the templates directive page. It’ll be fixed with the next release (because it’s generated from the code comments in the Caddy codebase).

For now, you can find it here:

For placeholders, you need to use the placeholder function, such as:

{{placeholder "http.error.status_code"}}

Note that err.status_code is a Caddyfile placeholder shortcut, so if you’re using placeholders outside the context of the Caddyfile itself (e.g. a template document) you need to use the long-form, i.e. http.error.status_code. You can find the shorthands here: Caddyfile Concepts — Caddy Documentation

1 Like
try_files {path}.html {path}
try_files {path} {path}/ =404

This doesn’t make sense. You shouldn’t have two try_files directives, they’ll step on eachother’s toes. Just define it once, with all the rewrite rules you need.

Seems like I didn’t the second rule anyway

rewrite error

The static site generator I’m using (astro.js) decided to start compiling /error.astro to /error/index.html after I did something, I am not sure why it does that yet.

For placeholders, you need to use the placeholder function, such as:

{{placeholder "http.error.status_code"}}

That solved the issue, thanks!

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.