1、首先新增一个api的异常错误类。
php artisan make:exception ApiExceptionHandler
2、修改\app\Exceptions\ApiExceptionHandler.php该文件为以下内容。
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Illuminate\Http\JsonResponse;
use Throwable;
class ApiExceptionHandler extends ExceptionHandler
{
//
public function render($request, Throwable $exception)
{
if ($this->isApiException($request, $exception)) {
$statusCode = $this->getStatusCode($exception);
$errorMessage = $exception->getMessage();
return new JsonResponse([
"code"=> "erro