flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.63k stars 409 forks source link

Argument #1 ($ex) must be of type Exception, TypeError given #485

Closed JmzTaylor closed 9 months ago

JmzTaylor commented 1 year ago

I am getting this error when trying to catch exceptions. This is the line causing this error.

Flight::map('error', function(Exception $ex) {});
JmzTaylor commented 1 year ago

Changing the code to this seems to have fixed it.

Flight::map('error', function(TypeError $ex) {});
mjisoton commented 1 year ago

As far as I know, the error function should accept any parameter that extends Exception and implements a Throwable. I couldn't replicate the error here, it worked normally with an Exception parameter.

<?php
require 'flight/Flight.php';

Flight::map('error', function(Exception $ex){
    echo $ex->getMessage();
});

Flight::route('/', function(){
    throw new ErrorException('Test ErrorException');    //It works
    throw new Exception('Test Exception');          //It works
    trigger_error('Test Trigger Error', E_USER_ERROR);  //It works
});

Flight::start();
?>

FlightPHP also converts all thrown errors into ErrorException. Tested with #v2.0.1.

Since TypeError extends Error, I guess there is a throw new Error() somewhere in your code, maybe? If I'm right, try using throw new ErrorException instead...