Laravel Basics Errors & Logging - fantasy0107/notes GitHub Wiki

η”’η”Ÿ exception

php artisan make:exception CardCreatedException

處理 exception

//controller 
use App\Exceptions\CardCreateException;
try {
    //...create card sql  
} catch(\Exception $exception) {
      throw new CardCreatedException('ζ–°ε’žε‘η‰‡ε€±ζ•—');
}

// handler
use App\Exceptions\CardCreateException;
use Illuminate\Validation\ValidationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;

public function report(Exception $exception)
{
    if ($exception instanceof CardCreateException) {
        // log
    }  elseif ($exception instanceof ValidationException) {
       // log validation η›Έι—œ
    } elseif ($exception instanceof ModelNotFoundException) {
        // log validation η›Έι—œ
    }
  

    parent::report($exception);
}