Can I have a template for server errors?

The way my templates and CSS are currently set up, when I get server errors they are obscured by the elements on the page. It would be handy if I could detect when there is an error and use a different template than the default one. Is this possible with Kirby?

Which kind of errors do you want to catch? Programming errors in your templates, issues while accessing external servers/APIs, etc.?

Just programming errors in my templates for now.

What you could do is to create a static HTML error page in the same design as your site and set it as Apache ErrorDocument in your .htaccess:

ErrorDocument 500 your_error_page.html

It will then catch server errors and PHP fatal errors.

The recommended way however is to test your website locally before publishing it. Programming errors are rather simple to find and fix.

Actually I am just working locally now and not using an Apache server, just the PHP built in server. I am talking about “Notice” and “Warning” messages from PHP that don’t entirely halt execution, along with echos and var_dumps that I do before the template is rendered. If I have a syntax error then it does just show me a plain white page with the error message. I realize that with echos outside the content section of my site they will always appear at whatever part of the template they are called from. I guess it is the same situation for the warnings and notices? I don’t know enough about PHP to know if it is possible to detect such things as they happen, wherever they happen and totally redirect code execution so that a different template could be rendered.

At any rate, I have come up with a workaround to use a modified “debugging template” while am working on some complicated stuff.

I definitely wouldn’t create a template just for warnings, for two reasons:

  • As I wrote, better fix the programming errors than have them on your production site in the first place (the warnings are meant to tell you what exactly is wrong so you can fix it). Not fixing them means having bugs. :wink:
  • Production servers normally don’t display warnings anyway, they are only logged to the error log.

Yes, warnings are printed where they occur. :slight_smile:

Yes, of course I will fix the errors. The point is I am developing some complicated scripts using components I am not entirely familiar with so I am producing errors constantly, and when I can’t read the errors it is hard to fix them! I was getting by with viewing the source code of the page to see the errors. I suppose I could look at logs, but that is just as much work. The most convenient thing is to see the errors right on the page. I never said anything about having the errors on a production server!

Anyway, I guess Kirby doesn’t have any special way to deal with this.

Well, if you want to see the errors, a special template would be even worse because it wouldn’t display errors at all.

Yes, the source view is helpful when debugging errors inside HTML tags for example.

I usually have the php error log open in console to view errors.

@texnixe When you said that it occurred to me that all error messages are automatically printed in the console where I am running the PHP server. I guess that is another quick way to see errors pretty clearly, although it doesn’t help for var_dump and such.