How to trigger a 404 error from within a plugin

One of my plugins does some processing and has to generate a 404 response to the user. How can this be done so the user sees the standard error page from the error plugin? I tried to do throw new \RuntimeException("Unable to load catalog resource ", 404); but this will not show the error plugin.

Hi @hmaier,

the cleanest way to issue a 404 error from within a plugin is by calling the onPageNotFound event i.e.


This will trigger the error page from the error plugin. If you want to show a customized error message you may include the following snippet into your plugin

$event = $this->grav->fireEvent('onPageNotFound');

$msg = "Unable to load catalog resource.";
if (isset($event->page)) {
} else {
  throw new \RuntimeException($msg, 404);

This is just one way implementing a custom error message in Grav. There are others and differ in the difficulty e.g. you can provide a custom error template wih a custom error md page and link to that or implement you own error page manager… Really there are no limits :slight_smile: