0

I need to save errors in my database. So I am trying to extend Error Handler in my custom module using plugin method. But it is not working. Here is my code

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\ErrorHandler">
        <plugin name="BridgeErrorNotificationErrorHandler" type="Bridge\ErrorNotification\Plugin\ErrorHandlerPlugin" sortOrder="10"/>
    </type>
</config>

ErrorHandlerPlugin.php

<?php
namespace Bridge\ErrorNotification\Plugin;

class ErrorHandlerPlugin
{
    protected $errorPhrases = [
        E_ERROR => 'Error',
        E_WARNING => 'Warning',
        E_PARSE => 'Parse Error',
        E_NOTICE => 'Notice',
        E_CORE_ERROR => 'Core Error',
        E_CORE_WARNING => 'Core Warning',
        E_COMPILE_ERROR => 'Compile Error',
        E_COMPILE_WARNING => 'Compile Warning',
        E_USER_ERROR => 'User Error',
        E_USER_WARNING => 'User Warning',
        E_USER_NOTICE => 'User Notice',
        E_STRICT => 'Strict Notice',
        E_RECOVERABLE_ERROR => 'Recoverable Error',
        E_DEPRECATED => 'Deprecated Functionality',
        E_USER_DEPRECATED => 'User Deprecated Functionality',
    ]; 

    public function afterhandler($errorNo, $errorStr, $errorFile, $errorLine)
    { 
        if (strpos($errorStr, 'DateTimeZone::__construct') !== false) {
        // there's no way to distinguish between caught system exceptions and warnings
          return false;
        }

       $errorNo = $errorNo & error_reporting();
       if ($errorNo == 0) {
         return false;
       }

       $msg = isset($this->errorPhrases[$errorNo]) ? $this->errorPhrases[$errorNo] : "Unknown error ({$errorNo})";
       $msg .= ": {$errorStr} inn {$errorFile} on line {$errorLine}";
       $msg .= "  just for testing";
       throw new \Exception($msg);
    }
}

To test this I am just adding $msg .= " just for testing"; in message.

4

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.