1

I am trying to override ErrorHandler class using preference but it is not working. di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
   <preference for="Magento\Framework\App\ErrorHandler" type="Pulsestorm\HelloWorldMVVM\Model\ErrorHandler"/>

</config>    

ErrorHandler.php

<?php
namespace Pulsestorm\HelloWorldMVVM\Model;

class ErrorHandler extends \Magento\Framework\App\ErrorHandler{
    public function getHandler()
    { echo"hii"; exit;
        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} in {$errorFile} on line {$errorLine}";
        throw new \Exception($msg);
    }
}    

I used echo exit to check preference is working or not. When any error or exception comes it doesn't echo and exit. Please let me know what I am doing wrong. Thank you in advance.

  • Have you found any solution? I am facing same issue – Rutvee Sojitra Jul 26 '18 at 13:03
1

class name was wrong, you mentioned Pulsestorm\HelloWorldMVVM\Model\Errors so it should be

<?php
namespace Pulsestorm\HelloWorldMVVM\Model;

class Errors extends \Magento\Framework\App\ErrorHandler{
    .....................
    .....................
    .....................
    .....................
}
  • Oh sorry my mistake but it is not working still. – Ramkishan Suthar Nov 8 '16 at 5:20
0

your preference in di.xml should be like this :

<?xml version="1.0"?>

  <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
      <preference for="Magento\Framework\App\ErrorHandler" type="Pulsestorm\HelloWorldMVVM\Model\ErrorHandler"/>

</config> 

As you have declared your class name same.

  • Yes I did the same but not working still – Ramkishan Suthar Nov 8 '16 at 5:23
  • remove var/ data and pub/static data, and then apply static content deploy and reindex – Ronak Chauhan Nov 8 '16 at 5:25
  • Ok let me check.. – Ramkishan Suthar Nov 8 '16 at 5:29
  • It's not working @Ronak – Ramkishan Suthar Nov 8 '16 at 5:48
  • how you have test? – Ronak Chauhan Nov 8 '16 at 5:54
0

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">
   <preference for="Magento\Framework\App\ErrorHandler" type="Pulsestorm\HelloWorldMVVM\Model\Errors"/>

</config> 

You have to declare classname ErrorHandler instead of Errors

 <?php
namespace Pulsestorm\HelloWorldMVVM\Model;

class Errors extends \Magento\Framework\App\ErrorHandler{
    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 handler($errorNo, $errorStr, $errorFile, $errorLine)
    {   
        echo"hii"; exit;
        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} in {$errorFile} on line {$errorLine}";
        throw new \Exception($msg);
    }
}
  • It's not working, I try this out earlier that's why i changed class name to Errors. – Ramkishan Suthar Nov 8 '16 at 5:22
  • any error are display? – Rakesh Jesadiya Nov 8 '16 at 5:24
  • I am generating an error to see is this working or not. Unfortunately it is not working. In my system.log it is saying that "main.INFO: Cache file with merged layout: LAYOUT_frontend_STORE1_4f0430021afacc712191c816b7a55d6dd and handles default, hello_mvvm_hello_world: Please correct the XML data and try again. [] []" – Ramkishan Suthar Nov 8 '16 at 5:27
  • Although I have created a another plugin in same di.xml file and it is working file. I don't know what is the problem in this preference. – Ramkishan Suthar Nov 8 '16 at 5:28
  • sorry i mean preference not plugin – Ramkishan Suthar Nov 8 '16 at 5:32
-1

Just remove di folder from Magento2Root/var do not compile again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.