Individual laravel exception handling

I am new to Laravel. I need to add a custom error handler. I tried adding the code below to my file global.php:

App::error(function(CustomException $exception, $code)
{
echo 'Debug: CustomException<br/>';
});

To throw this error, I used the code below in one of my controllers:

throw new CustomException();

But I get an error because CustomException was not found.

I searched for it for a solution, but everywhere I find the same solution.

Please help me fix this.

+4
source share
2 answers

First you need to define your custom exception. At the top of your file, global.phpadd the following:

class CustomException extends Exception {}

Of course, you can add special properties and methods to this class.

+2

(: //MyCustomException.php), composer.json.

"autoload": {
  "files":["app/Exceptions/MyCustomException.php]
}

: composer dumpautoload

MyCustomException.

-

<?php 

class MyCustomException extends \Exception {}
+2

Source: https://habr.com/ru/post/1584272/


All Articles