The following is untested and unfinished, but I hope it might get you going:
In Craft CMS, there is an event "EVENT_BEFORE_HANDLE_EXCEPTION", which you could use to handle exceptions. With a plugin or a module, you could do something like:
Event::on(
ErrorHandler::className(),
ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION,
function(ExceptionEvent $event) {
$this->myPlugin->processException($event->exception);
}
);
And in your plugin services, you have that function 'processException' that should be able to read the resulting status code and act accordingly
use Craft;
use craft\base\Component;
use yii\base\Exception;
class myService extends Component
{
public function processException($exception)
{
$lastStatusCode = $exception->statusCode ?? null;
// check for errorcodes, i.e. 400, then do something
Lastly, if you wish to have Craft send you emails, there is a built-in function for that
public function sendErrorMail($e, $myerror, $url = null)
{
Craft::$app->mailer->send($this->getErrorMessage($e, $myerror, $url));
}
getErrorMessage just puts together the email contents and is reusable
public $senderEmail = "[email protected]"; // or get craft website email
public $adminEmail = "[email protected]"; // or get craft admin unser email
public function getErrorMessage($e, $myerror, $url = null)
{
if (!empty($e)) {
$message = $e->getMessage();
} else {
$message = null;
}
{# might need to set temp view path with $this->view->setTemplatesPath(ModuleName::$instance->getBasePath());#}
$body = $this->view->renderTemplate('/templates/email/error.twig',
array("exceptionMessage" => $message, "error" => $myerror, "url" => $url));
$senderFull = array("[email protected]" => Craft::t('module-handle','www.doe.com Website'));
$message = new Message();
$message->setFrom($senderFull);
$message->setReplyTo($this->senderEmail);
$message->setTo($this->adminEmail);
$message->setSubject("Error on www.doe.com");
$message->setHtmlBody($body);
return $message;
}