I am trying to implement error report in magento 2 so for that purpose i would like to mail all the errors in email. So I started with prefernece.


<?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"/>


namespace Pulsestorm\HelloWorldMVVM\Model;

 * An error handler that converts runtime errors into exceptions
class ErrorHandler 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);

I try echo exit at top of class (after namespace) but it showing nothing it means class is not calling. Is there any mistake in di.xml.


Error handler created not through DI, so preferences are not applicable

see: https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/App/Bootstrap.php#L373

The best way to log errors its change settings for \Psr\Log\LoggerInterface that already used to log errors. Magento uses Monolog library for logging. For example, you can add \Monolog\Handler\NativeMailerHandler in DI (see: https://github.com/magento/magento2/blob/develop/app/etc/di.xml#L183)

But if you steel require to change ErrorHandler, you have only one way to do it. Copy class to your module and change as you need. Then add in your registration.php include of this file: like include(__DIR__. 'path/to/ErrorHandler.php')

  • What should i do that for this purpose. I am not getting anything. Please help @KAndy – Ramkishan Suthar Nov 9 '16 at 8:41
  • I update post with details – KAndy Nov 9 '16 at 19:01

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.