3

Hi everyone! In my Laravel application I have a upload function for an Excel file. I got this code from the web and adjusted it to my application. The problem is, it doesn't catch a fatal error exception which is produced when the user submits a file, but hasn't selected a file. I don't understand why it is not being caught. I will add a part of my controller.

 public function upload() { $file = array('thefile' => Input::file('thefile')); $rules = array('excel' => 'excel'); $validator = Validator::make($file, $rules); if ($validator->fails()) { return Redirect::to('UploadExcelFile')->withInput()->withErrors($validator); } else { try{ // FatalErrorException happens in this line! if (Input::file('thefile')->isValid()) { $destinationPath = 'uploads'; $fileName = Input::file('thefile')->getClientOriginalName(); Input::file('thefile')->move($destinationPath, $fileName); Session::flash('success', 'Upload successfully'); $fileNameJSON = exec("python /path/to/script/ExcelToJSON3.py $fileName"); // This returns a full path name of the JSON file that is made... if ($fileNameJSON == null){ return Redirect::to('/dashboard/input'); } else { // Getting the ID from the file that is uploaded try { $jsonDecode = json_decode(file_get_contents($fileNameJSON)); } catch (ErrorException $e){ return Redirect::to('/errorpage')->with(array('status'=> 'ErrorException')); } A lot of code for handling the data entered.... }catch (FatalErrorException $e){ return Redirect::to('/errorpage')->with(array('status'=> 'FatalErrorException')); } } return true; } 

The error that is given:

FatalErrorException in UploadExcelFileController.php line 35: Call to a member function isValid() on a non-object

So, I don't understand why this code doesn't handle the error exception and how I could fix this!

2
  • Are you importing Symfony\Component\Debug\Exception\FatalErrorException at the top of the class as use Symfony\Component\Debug\Exception\FatalErrorException? Commented Nov 15, 2016 at 8:45
  • Yes! I have imported the FatalErrorException with the whole path name, without the whole path name and like @Andy had suggested. But I still get the same error.. Commented Nov 15, 2016 at 10:43

1 Answer 1

1

Unless you've imported the namespace that FatalErrorException is declared in with "use" you will need to scope the exception, like this:

catch (\Symfony\Component\Debug\Exception\FatalErrorException $e) { 

Otherwise you're using whatever namespace your class is in and trying to catch an exception that is declared in that namespace. It looks like your ErrorException is similarly set up.

I'm not sure that the namespace above is the one your class is deriving from, I'm just guessing it is because you're using Laravel.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi! I have imported the namespace at the top as 'use FatalErrorException', I have tried your suggestion but I still get the error.. Do you think it could be because of the order?
Which version of PHP are you using? Calling a method on a non-object is actually an engine level fatal error and I suspect what is happening is that the FatalErrorException is being caught by a function that is handling those errors. Only PHP7 will throw fatal exceptions as exceptions.
PHP 5.5.9-1ubuntu4.19 (cli)
Yeah it's actually a fatal error engine error and not an exception that is occurring when you call a method on a non-object. I think that the exception that you're seeing is being thrown in a custom error handling class that runs on shutdown after being set up with register_shutdown_function(). In my opinion your best bet is to first check that the thing you're working with is an object before calling methods on it. You can try using is_object() or even getting its class to be sure. In PHP7 you would be able to catch (Error $e) to catch that particular error.
Thank you for explaining this!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.