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.

3
  • Have you found any solution? I am facing same issue Commented Jul 26, 2018 at 13:03
  • There is no method getHandler in that class.
    – Black
    Commented Apr 23, 2021 at 14:38
  • Same issue faced, is there any solution for it...? Commented Apr 2, 2022 at 4:36

5 Answers 5

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{
    .....................
    .....................
    .....................
    .....................
}
1
  • Oh sorry my mistake but it is not working still. Commented Nov 8, 2016 at 5:20
1

Check to see that someone else has already preferenced the targeted class:-

find vendor/ -name "di.xml" -exec egrep "ErrorHandler" {}  \; 

and try to scan your custom code folder:

find app/code -name "di.xml" -exec egrep "ErrorHandler" {}  \; 
1

I've encountered a similar kind of problem, as the preference was not working.

While debugging I found there were some private properties in the original class file.

Then I added all those private properties as protected into the preferenced/over-rided file and then it worked.


Maybe this can be helpful for someone.

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.

14
  • Yes I did the same but not working still Commented Nov 8, 2016 at 5:23
  • remove var/ data and pub/static data, and then apply static content deploy and reindex Commented Nov 8, 2016 at 5:25
  • Ok let me check.. Commented Nov 8, 2016 at 5:29
  • It's not working @Ronak Commented Nov 8, 2016 at 5:48
  • how you have test? Commented Nov 8, 2016 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);
    }
}
18
  • It's not working, I try this out earlier that's why i changed class name to Errors. Commented Nov 8, 2016 at 5:22
  • any error are display? Commented Nov 8, 2016 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. [] []" Commented Nov 8, 2016 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. Commented Nov 8, 2016 at 5:28
  • sorry i mean preference not plugin Commented Nov 8, 2016 at 5:32

Your Answer

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

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