Slim framework 3 – Passing Custom Headers to error pages

So during work today I ran into an issue. There were a lot of users connecting and some pages were showing Slim Frameworks custom error pages.

A website error has occurred. Sorry for the temporary inconvenience.

Now you’ve seen this error from time to time and usually it’s not a huge problem, for us that issue resulted in too many connections to our database at once while the page cache was rebuilding. Usually this fixes itself (and I probably should add a workaround to that soon), but we’re using Litespeed Cache. Litespeed sucks, not because it’s bad software, but because when shit hits the fan litespeed can cache it and it’s very painful to deal with. Which in our case it was caching that slim error page “website error has occurred”. That’s no good so I needed to tell litespeed not to cache the page when this happens.

Great, slim framework supports custom error handlers. Problem is I didn’t want to implement a completely new template. I just wanted to modify the headers to the error handler we already have; mostly because we don’t have a custom template for this page yet. So looking at the source the default classes are Error and PhpError. These classes handle the default error messages and headers. Perfect, so in order to do what I wanted we just extend those classes into our own handlers then pass that class to the custom error handlers in the container and override the default functionality. Win/win.