Catch FatalErrorException PHP - php

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!

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.

Related

Catch errors with Laravel API Resource

I'm trying to use Laravel API Resource and handle the error message by sending a specific HTTP code
Here is my code :
public function show($id)
{
try {
return FruitResource::make(Fruit::find($id));
}
catch(Exception $e)
{
throw new HttpException(500, 'My custom error message');
}
}
My try/catch is systematically ignored when I try to access the route.
I am voluntarily accessing an object that is not in the database. I have ErrorException with message Trying to get property 'id' of non-object.
I would like to be able to send my own Exception here, in case the user tries to access data that doesn't exist. And return a json error.
Try this (notice the \ before Exception):
public function show($id)
{
try {
return FruitResource::make(Fruit::find($id));
}
catch(\Exception $e)
{
throw new HttpException(500, 'My custom error message');
}
}

Fatal Error on using $exception variable in render() Method in App\Exceptions\Handler.php

I am new to Laravel and have an Issue regarding the Handler.php File.
I am trying to create a class that takes an exceptions and transforms it into a JSON Response.
Sadly though, upon calling the constructor a series of Errors are thrown:
(ErrorErrorErrorErrorErrorErrorErrorErrorErrorErrorErrorSymfony\Component\ErrorHandler\Error\FatalError)
My code:
render() in Handler.php:
public function render($request, Throwable $exception)
{
$errorResource = new ErrorResource($exception);
return $errorResource->getJsonResponse();
}
class ErrorResource in ErrorResource.php:
<?php
namespace Transformers;
use Throwable;
class ErrorResource
{
private $exception;
private $defaultCodes = [TypeError::class => 400];
private $defaultMessages = [TypeError::class => 'Untgültige URL Parameter'];
function __construct(Throwable $exception)
{
$this->exception = $exception;
}
public function getJsonResponse($exception)
{
$codeToThrow = 500;
$messageToThrow = "Internal Server Error";
$type = get_class($this->exception);
if (empty($exception->getCode())) {
$codeToThrow = $this->defaultCodes[$type];
} else {
$codeToThrow = $exception->getCode();
}
if (empty($exception->getMessage())) {
$messageToThrow = $this->defaultMessages[$type];
} else {
$messageToThrow = $exception->getMessage();
}
return response()->json(array(
'Type' => $type,
'Message' => $messageToThrow
), $codeToThrow);
}
}
I have also tried to move the method getJsonResponse() to the Handler.php file and call it from there, but without any luck.
I am really confused as to why I am not allowed to do certain things with the $exception variable (I have also tried to create a clone of this object - but the same error occures)
I hope you can help me resolving this issue,
Greetins,
Franz
The issue is, that PHP is call by value. That is why it is implicitely trying to clone an unclonable object -> Error. To resolve this issue one can use wrapper objects, but I decided to simply use call by reference (https://www.javatpoint.com/php-call-by-reference)

Nested Laravel Try catch not throwing inner Exception, only outer

I'm a little bit fuzzy with Try Catch blocks, up until now i've just put some code inside them and tried to catch the error. I'm now using them in Laravel but I cant seem to get the exception to fire correctly when nesting statements, and i'm just wondering if someone can explain why or point me in the right direction!
I have 3 models, cases, members and documents.
Heres what I want to achieve
Try to store cases, members and any documents upload (including moving documents to storage folder). If any fail, revert everything.
I am running the cases and members store inside a DB::transaction function which is working great, but I then want to run my document move/store in the document model externally.
When I save my form, the case store function is whats fired first.
Here are my models/controllers so far
Case Controller
public function store(AddCaseRequest $request)
{
//Try to create a new case
try{
//New Case
$case = new Cases;
//New Member
$member = new Members;
DB::transaction(function() use ($case, $member) {
//Save Case
$case->fill(request()->all())->save();
//Save Member
$member->fill(request()->all())->save();
//Documents
if(request()->has('document')){
//Loop the documents and store
foreach(request()->document as $doc){
$document = with(new Documents)->storeNewDocument($doc, $case->id, 'case', 'cases');
}
}
}, 3);
//Redirect back to view page
return redirect()->route('cases.view', [$case->id]);
}
//Catch the error
catch(\Exception $e){
//Log the error
Log::debug('Cases Create Error', (Array) $e->getMessage());
//Redirect back
return redirect()->back() //Redirect back
->withErrors(['Whoops! Something went wrong, please try again.']) //Send an error message
->withInput(request()->all()); //Send the inputs back
}
}
Document Model
public function storeNewDocument($file, $id, $type, $directory)
{
//Check the directory exists
if($this->checkDirectory($directory)){
//Get the file extention
$extension = $file->getClientOriginalExtension();
//Generate a new filename
$newName = md5(uniqid(rand(), true)) . "." . $extension;
//Move the file
try {
//Try to move the file
Storage::disk('local')->putFileAs($directory, $file, $newName);
//Create the new record
$document = new $this;
$document->type = $type;
$document->foreign_id = $id;
$document->nice_name = $file->getClientOriginalName();
$document->name = $nwName;
$document->save();
} catch (Exception $e){
dd($e);
//Log the error
Log::debug('Document Move Error', (Array) $e->getMessage());
//Try to delete the file incase the document save failed
Storage::delete($directory . '/' . $newName);
//Return false
return false;
}
//All moved, return the new name
//return $newName;
}
}
I have faked an error in the Document storeNewDocument function, by trying to call $nwName instead of $newName when saving the document to force an error.
What currently happens is
Case and member do not create (which is correct I believe because there is an error being thrown somewhere so the transaction doesn't complete?)
The file moves successfully in the Document storeNewDocument function, and lands in the storage/cases folder, which is correct.
The document then fails to save, but the dd($e) inside the catch on the document model is not hit?
At this point the document, case and member DB records have not saved, but the catch doesn't fire at all in the document model, so I cannot find the file and delete it?
I'm really not sure on how nested try/catch statements work. Can someone please let me know why the catch on my Document Model is not firing or if i'm trying to achieve my goal in a completely stupid way?!
Any sort of clarification would be massively appreciated!
Note - The $this->checkDirectory() function simply just checks if the function exists and create it if it doesn't, there is no issue with this function, just didn't see the point in adding it in to the question.
update - The error seems to be whenever an error is thrown inside the document model functions, it always hits the catch in the Case store function first. For example, if I got rid of the dd($e) function in the catch, in my log file, the log message is not Document Move Error, it is always Cases Create Error, which to me says that the document storeNewDocument catch is never being hit?
I think you just need to rethrow your exception.
} catch (Exception $e){
//Log the error
Log::debug('Document Move Error', (Array) $e->getMessage());
//Try to delete the file incase the document save failed
Storage::delete($directory . '/' . $newName);
//throw $e
throw $e;
}
You are returning false from this catch block, which is inside another try/catch block.

How can I check if file exists with exception handeling

I am trying to use exception handling in case a file does not exists. For example when I run the model method and pass a string usr (which I know there is no file with that name) . It gives me the following error message
Fatal error: Uncaught exception 'Exception' with message 'Usr.php was not found' in /app/core/controller.php on line 14
I can't figure out whats wrong here. Can someone please help me figure this out?
Below is my code. Thanks alot!
class Controllers{
public function model($model){
if(!file_exists("../app/models/".$model.".php")) {
throw new exception("{$model}.php was not found");
}
try {
require ("../app/models/".$model.".php");
} catch(Exception $e) {
echo $e->getMessage();
}
return new $model();
}
}
You can't throw an exception without catching it; this automatically causes the PHP script to crash. So, you need to surround your entire function in the try-catch block, or the "model not found" exception will be uncaught. Your code should be something like this:
<?php
class Controllers {
public function model($model){
try {
if (!file_exists("../app/models/".$model.".php")) {
throw new Exception("{$model}.php was not found");
}
require ("../app/models/".$model.".php");
} catch(Exception $e) {
echo $e->getMessage();
}
return new $model();
}
}
Never mind guys! I found out I needed to use the try/catch blocks in the file where my method is being invoked
Example..
class Home extends Controllers{
public function index($name = ""){
try{
$user = $this->model('Usr');
}catch (Exception $e){
echo $e->getMessage();
}
//var_dump($user);
}

How to catch an exception from another class method PHP

I'm having trouble catching an exception in PHP
Here's my code.
try {
require $this->get_file_name($action);
}
catch (Exception $e) {
//do something//
}
and the method being called
private function get_file_name($action) {
$file = '../private/actions/actions_'.$this->group.'.php';
if (file_exists($file) === false) {
throw new Exception('The file for this '.$action.' was not found.');
}
else {
return $file;
}
}
Resulting in:
Fatal error: Uncaught exception 'Exception' with message $action was not found.'
Exception: The file for this $action was not found.
However If I put a try-catch block inside of the function and call the function, I'm able to catch the exception no problem.
What am I doing wrong?
If you are catching the Exception inside a namespace, make sure that you fall back to the global namespace:
...
}(catch \Exception $e) {
...
}...
You can also have a look at the following resources:
Why isn't my Exception being caught by catch?
http://php.net/manual/en/language.exceptions.php, top note by user zmunoz
I can't see all of the class body but If You want to use method out of the class it should be Public not Private.
Try to check if file You trying to get exists:
var_dump($file = '../private/actions/actions_'.$this->group.'.php');
IMO there is no file in this path

Categories