Stop laravel from including trace within an exception
I have my own exception extension that, when called, I want it to output a message to the browser. However, when I throw this exception, catch it and print the message, but the message is a traceback.
Here is my global.php:
class ApiException extends Exception {}
App::error(function(ApiException $ex){
dd($ex->getMessage());
});
My code snippet:
try {
if (!Input::get('password')) {
throw new Exception('Password not set');
}
if (User::all()->count()) {
throw new Exception('User already exists');
}
Artisan::call('db:seed', [
'--class' => 'VerifyUserSeeder'
]);
$User = \Toddish\Verify\Models\User::find(1);
$User->password = Input::get('password');
$User->save();
} catch (Exception $ex) {
throw new ApiException($ex);
}
Exit to the browser:
exception "Exception" with message "Password not set" in /Users/kevin/Documents/Web/app/controllers/Settings/SetupController.php:8 Stack trace: # 0 [internal function]: SetupController-> setupPassword () 1 / Users / kevin / Documents / Web / vendor / laravel / framework / src / Illuminate / Routing / Controller.php (231): call_user_func_array (array, array) # 2 .......
source to share
After re-nesting the try catch nesting and throwing the ApiException directly, as Fabio asked and suggested, this solved the problem. Now my code looks like this:
if (!Input::get('password')) {
throw new ApiException('Password not set');
}
if (User::all()->count()) {
throw new ApiException('User already exists');
}
try {
Artisan::call('db:seed', [
'--class' => 'VerifyUserSeeder'
]);
$User = \Toddish\Verify\Models\User::find(1);
$User->password = Input::get('password');
$User->save();
} catch (Exception $ex) {
throw new ApiException($ex);
}
source to share
Can you explain why you are throwing generic exceptions, catching them, and then throwing a new exception again?
Why don't you just just throw the ApiException?
if (!Input::get('password')) {
throw new ApiException('Password not set');
}
if (User::all()->count()) {
throw new ApiException('User already exists');
}
Artisan::call('db:seed', [
'--class' => 'VerifyUserSeeder'
]);
$User = \Toddish\Verify\Models\User::find(1);
$User->password = Input::get('password');
$User->save();
source to share