Skip to content

Extending the error handler doesn't work for developer thrown exceptions #9931

@ebrigham1

Description

@ebrigham1

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.3.10

I'm new to cake but not new to programming. I'm trying to extend the cake error handler to add additional behaviors. I'm looking to do things like email the dev team with exception information, or contact an API to generate a ticket, ect.

namespace App\Error;

use Cake\Error\ErrorHandler as CakeErrorHandler;
use Exception;

class ErrorHandler extends CakeErrorHandler
{
    public function handleException(Exception $exception)
    {
        // Add emailing exception functionality
        $this->_emailException($exception);
        // Use parent functionality to log/display exception ect
        parent::handleException($exception);
    }
    // Code to email exceptions, throttle emails,
    // skip emails by exception class, ect
}

The issue I'm running into is that this class extension will work for all fatal errors that get wrapped up into exceptions, however it doesn't work for exceptions that a developer might choose to throw in the application since they seem to be caught, logged and displayed via Cake\Error\Middleware\ErrorHandlerMiddleware rather than Cake\Error\ErrorHandler

For instance:

namespace App\Controller;

use App\Controller\AppController;

class PaymentController extends AppController
{
    public function fulfillOrder()
    {
        // Process payment was successful
        // Attempt to fulfill payment, requires API calls to external resources
        // that fail
        throw new App\Exception\FulfillmentFailed('Order failed');
    }
}

Why aren't these types of exceptions also caught and handled by Cake\Error\ErrorHandler? When removing Cake\Error\Middleware\ErrorHandlerMiddleware from my local application middleware stack defined in App/Application I notice no difference in logging or rendering of those types of exceptions. So it seems like its largely doing the same job Cake\Error\ErrorHandler already does. Yet it forces me to also extend and override the middleware if I want to be able to add my functionality to those types of exceptions. Is there a reason these types of exceptions are caught in this manner rather than being allowed to bubble through to Cake\Error\ErrorHandler?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions