[Solved] PHP Errors and exception with trace

I’ve defined the following entry for the phpErrorHandler:

$container['phpErrorHandler'] = function($container) {
    return function($request, $response, $exception) use ($container) {
        $settings    = $container->get('settings');
        $defaultDest = $settings['defaultRedirectTarget'] ?? 'https://someurl.com/blah/';
        $exMessage   = $exception->getMessage();
        $logMessage  = "PHPErrorHandler: $exMessage<br />" . var_dump($exception->getTrace());
        $container->get('logger')->error($logMessage);
        return $response->withRedirect($defaultDest);
    };
};

The curious thing is that no stack trace is printed. Is there anything special about PHP errors I’m missing?

Note: The exactly the same code prints a stack trace when used for the ‘normal/standard’ errorHandler.

Thanks for any insights.

Could you please show us more code (especially where the error occurs)?

I don’t have an error, I tested this by inserting the following into my code

$nonExistingVariable->blah();

Which gave me the correct error message but no stack trace.

Notice: Undefined variable: nonExistingVariable in /in/q5OsX on line 3
Fatal error: Uncaught Error: Call to a member function blah() on null in /in/q5OsX:3

But even then the stack trace is not empty: https://3v4l.org/S1Th4

  • Are you using any other middleware that handles exceptions / throwables / errors etc?
  • Do you overwrite the default PHP error handler with set_error_handler?
  • Are you using Slim 3 or Slim 4?
  • Do you call $nonExistingVariable->blah(); inside or outside a Slim route?

Hi, in answer to your questions:

Are you using any other middleware that handles exceptions / throwables / errors etc?

No.

Do you overwrite the default PHP error handler with set_error_handler?

Not that I know of. This is hard to know for sure because of the composer packages I import/use, but my code does not do this and I’m pretty sure the other packages also don’t do this.

 Are you using Slim 3 or Slim 4?

Slim 3.12.1

Do you call  `$nonExistingVariable->blah();`  inside or outside a Slim route?

Inside a Slim route. I just added it my home controller code for test purposes.

Thank you for your help, it’s much appreciated.

not sure if I understand that correctly but if you are trying to “print” backtrace to some logger, you can not use var_dump but for example var_export($backtrace, true) where true in var_export specifies that you want to return value/dump as string

(var_dump returns null/void so by concatenating string with var_dump result you will always receive empty part from var_dump)

not sure if I understand that correctly but if you are trying to “print” backtrace to some logger, you can not use var_dump but for example var_export($backtrace, true) where true in var_export specifies that you want to return value/dump as string

Your comment pointed me in the right direction, thank you so much!!! As often in programming, the dumbest mistakes are the hardest ones to figure out without a second (or third) pair of eyes.

Changing the debug handler code to use

$exception->getTraceAsString();

makes everything work just right. Sorry for having been stupid :grinning: