Framework: [PHP 7] Exception error PHP 7

Created on 12 Jul 2015  路  35Comments  路  Source: laravel/framework

I got error in app/Exeptions/Handler.php

Just testing on PHP and don`t know where report issues about PHP 7
Error I have:
Uncaught TypeError: Argument 1 passed to AppExceptionsHandler::report() must be an instance of Exception, instance of TypeError given, called in

This error where thrown when I make typo in controller
return view('notices,create');
P.S. Any way Laravel good prepared for PHP7. I test for 2 week now and only this issue ))

Most helpful comment

Here's my take, for laravel 4.2. Couldn't pass the $previous parameter since Whoops also expects Exception instead of Throwable, so this limits the usefulness of the error message, but it sure beats something that looks like an internal error.

--- a/laravel/framework/src/Illuminate/Exception/Handler.php
+++ b/laravel/framework/src/Illuminate/Exception/Handler.php
@@ -143,6 +143,16 @@
         */
        public function handleException($exception)
        {
+               if ( ! $exception instanceof Exception) {
+                       $exception = new ErrorException(
+                               $exception->getMessage(),
+                               $exception->getCode(),
+                               E_ERROR,
+                               $exception->getFile(),
+                               $exception->getLine()
+                       );
+               }
+
                $response = $this->callCustomHandlers($exception);

                // If one of the custom error handlers returned a response, we will send that

I know the combination of laravel 4.2 and php7 is unsupported, but if this patch is okay, can I PR it?

All 35 comments

After research look like need use "Throwable" instead or for support php5 to
try {
// Code that may throw an Exception or Error.
} catch (Throwable $t) {
// Executed only in PHP 7, will not match in PHP 5.x
} catch (Exception $e) {
// Executed only in PHP 5.x, will not be reached in PHP 7
}

I don't think it would be a good idea for us to start changing all our method signatures in 5.1, but perhaps 5.2 would be a really good place to get this sorted out.

Ping @taylorotwell. This is going to be pretty important for is going forward into php 7.

I'm actually going to take a proper look at this. :)

@GrahamCampbell So I well report like [PHP7][5.2] blabla in header? It is ok for now?

It's fine. We only enforce that on PRs.

We do that so people can easily search through PRs since github doesn't support searching by branch.

What about add label php7 ?

Any way Laravel good prepared for PHP7. I test for 2 week now and only this issue ))

There will be more issues than this regarding throwable, I'm pretty sure.

Nice work!

hello i got some error on php7 that may be related to this issue

Uncaught TypeError: Argument 1 passed to App\Exceptions\Handler::report() must be an instance of Exception, instance of Error given

FatalErrorException in Handler.php line 25: Uncaught TypeError: Argument 1 passed to App\Exceptions\Handler::report() must be an instance of Exception, instance of Error given, called in D:\xampp\htdocs\test\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\HandleExceptions.php on line 73 and defined in D:\xampp\htdocs\test\app\Exceptions\Handler.php:25
Stack trace:
#0 D:\xampp\htdocs\test\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\HandleExceptions.php(73): App\Exceptions\Handler->report(Object(Error))
#1 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleException(Object(Error))
#2 {main}
thrown

have you seen this error?

same as @peter-mw, any hot fix for 4.2?

any hot fix for 4.2?

No. 4.2 is not maintained. Upgrade to 5.1 LTS, or 5.2.

hello i got some error on php7 that may be related to this issue

Laravel 5.0 and bellow DO NOT support PHP 7.

I'm having an issue with Laravel 5.1 and PHP7. Is this related? Should I make a homestead box using 5.5 to get around this?

Laravel 5.1 supports php 7.

Make sure you yourself don't pass throwables to the exception handler though.

While I'm upgrading our system to the 5.1LTS version, for the time being I catch the throwables in the index.php and artisan files and re-throw them as exceptions.

/** @todo Upgrade to laravel 5.1 to fix this!! */
try {
    $app = require_once __DIR__.'/../bootstrap/start.php';
    $app->run();
} catch(Throwable $e) {
    throw new \Exception($e->getMessage()."\n".$e->getTraceAsString(), $e->getCode());
}

That won't fix all cases though.

FatalErrorException in Handler.php line 34:
Uncaught TypeError: Argument 1 passed to AppExceptionsHandler::report() must be an instance of Exception, instance of Error given, called in C:xampphtdocsauthenticationvendorlaravelframeworksrcIlluminateFoundationBootstrapHandl
Can anyone help me...This error show me i m using laravel 5 framework

eExceptions.php on line 73 and defined in C:xampphtdocsauthenticationappExceptionsHandler.php:34
Stack trace:

0 C:xampphtdocsauthenticationvendorlaravelframeworksrcIlluminateFoundationBootstrapHandleExceptions.php(73): AppExceptionsHandler->report(Object(Error))

1 [internal function]: IlluminateFoundationBootstrapHandleExceptions->handleException(Object(Error))

2 {main}

thrown

Here's my take, for laravel 4.2. Couldn't pass the $previous parameter since Whoops also expects Exception instead of Throwable, so this limits the usefulness of the error message, but it sure beats something that looks like an internal error.

--- a/laravel/framework/src/Illuminate/Exception/Handler.php
+++ b/laravel/framework/src/Illuminate/Exception/Handler.php
@@ -143,6 +143,16 @@
         */
        public function handleException($exception)
        {
+               if ( ! $exception instanceof Exception) {
+                       $exception = new ErrorException(
+                               $exception->getMessage(),
+                               $exception->getCode(),
+                               E_ERROR,
+                               $exception->getFile(),
+                               $exception->getLine()
+                       );
+               }
+
                $response = $this->callCustomHandlers($exception);

                // If one of the custom error handlers returned a response, we will send that

I know the combination of laravel 4.2 and php7 is unsupported, but if this patch is okay, can I PR it?

No, that still isn't sufficient I'm afraid. I made over 40 changes on 5.1 to get it to work properly in every case.

4.2 is closed for all changes except security fixes.

@dequis Your solution worked for Laravel 5.0.

That solution superficially works, but doesn't fix all cases. For example, if you had a throwable occur inside rendering a view, this wouldn't work.

Perhaps, but I think that all exceptions are handled through the HandleExceptions class, if there are exceptions that could be handled outside of it then putting that condition on the received Error or Exception object in all handling classes will solve the problem.

I'm having the same issue after upgrading from 5.1 to 5.3, and using PHP7, any tips?

Edit: Forget it, I was running my local webserver off of an old clone...

i had same problem in laravel 5.1. After reading this issue, i found out i was using laravel v5.1.0 and that seemed wrong so changed 5.1.0 to 5.1.* and ran composer update. this solved the issue for me.

use
if (!$e instanceof Exception) {
$e = new ErrorException(
$e->getMessage(),
$e->getCode(),
E_ERROR,
$e->getFile(),
$e->getLine());
}
in vendor/compiled.php line 1720 handleException function

basith374 answer has worked for me

If using Laravel 4.2, upgrade to Laravel 4.2.20. It resolves PHP 7 issues.

Hi,
I found some problems with handling errors in php 7.1 laravel 5.5. I use imap, and sometimes it throws an error. The HandleExceptions class can catch the error, but what it does is throwing new ErrorException:

public function handleError($level, $message, $file = '', $line = 0, $context = []) {
if (error_reporting() & $level) {
throw new ErrorException($message, 0, $level, $file, $line);
}
}

this new exception is not handled by my app/Exceptions/Handler class. Instead of rendering nice error screen (i have APP_DEBUG = TRUE ) i just get this:

The page cannot be displayed because an internal server error has occurred.

The exception is thrown, because when i changed the code to:

public function handleError($level, $message, $file = '', $line = 0, $context = []) {
if (error_reporting() & $level) {
$e = new ErrorException($message, 0, $level, $file, $line);
$this->handleException($e);
throw $e;
}
}

than my app/Exceptions/Handler gets executed.

What are pros and cons for that change? Should i override HandleExceptions class?
Why is that thrown new ErrorException not handled like other thrown exceptions? If i throw an Exception in Controller, it gets handled.

replace AppExceptionsHandler with this
use Throwable;

public function report(Throwable $exception);
public function render($request, Throwable $exception);
and session.php with
'secure' => env('SESSION_SECURE_COOKIE', null),

'same_site' => 'lax',

Was this page helpful?
0 / 5 - 0 ratings