I have been trying to implement some custom Exceptions in Laravel 5.3 with no luck.
I have created an AppException which extends the Exception class. The idea of this is that I would like to attach a user to my exceptions so I know who tripped it and can log it.
So I set up my custom exception like this:
class AppException extends Exception{
protected $userId;
public function __construct($message = '', $userId=0)
{
parent::__construct($message);
$this->userId = $userId;
}
}
Now what I would like is to use try catch's in my controller functions and then catch an exception and then rethrow the app exception so I can attach the user. Like this:
try{
<< code here >>
}
catch(Exception $e){
throw new AppException($e->getMessage, $request->user()->id);
}
What I am finding is that I am unable to get a good trace stack because the line I am logging from my exception is the line from the rethrow in the catch, not from the actual exception.
What would be the right way to set this up? I am trying to do this in a way where I can utilize the built in Handler.php file that comes with Laravel instead of having to put in log code in every try catch.
Thanks!