TransWikia.com

How to receive email notification when an error or bad request happens?

Craft CMS Asked on June 9, 2021

Is it possible to receive email notifications when there is an error or Bad Request issue in Craft CMS?

We had an issue where our contact forms were not working due to craft.app.config.general.[csrfTokenName] changing to craft.app.config.general.csrfTokenName. No warnings in the deprecated code section. Instead, potential customers would see a Bad Request message, but we had no idea for days.

I would like to be alerted if such an error occurs so I can fix the issue much quicker.

Similarly, if there is an error on the site anywhere, I would like to be alerted.

Is it possible?

Thanks

2 Answers

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 craftbaseComponent;
use yiibaseException;

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;
}

Answered by MisterMike on June 9, 2021

I'd recommend using a service like Sentry or Bugsnag that are tailored exactly for this situation and include some Craft integrations as well:

https://plugins.craftcms.com/search?q=sentry

https://plugins.craftcms.com/search?q=bugsnag

Answered by Brad Bell on June 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP