01) 添加異常處理類
02) 修改laravel 異常處理
03) 拋出異常
01) 添加異常處理類
<?php //app/Exceptions/V1Exception.php namespace App\Exceptions; use Throwable; class V1Exception extends \Exception { function __construct(string $message = "", int $code = 0, Throwable $previous = null) { parent::__construct($message, $code, $previous); } }
02) 修改laravel 異常處理
//app/Exceptions/Handler.php public function render($request, Exception $exception) { if ($exception instanceof V1Exception) { $result = [ "msg" => $exception->getMessage(), "data" => '', "status" => 0 ]; return response()->json($result); } return parent::render($request, $exception); }
03) 拋出異常
throw new V1Exception("我要拋出異常");