- <?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);
- }
- }
修改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);
- }
抛出异常
- hrow new V1Exception("我要抛出异常");