希望大佬發現不合理的地方可以給我點建議
版本:laravel 7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Illuminate\Support\Facades\Log;
public function render($request, Throwable $exception)
{
if (request()->segment(1) == 'api') {
switch (true) {
case $exception instanceof UnauthorizedHttpException:
return response()->json([
'code' => 401,
'message' => '登陸已過期,請重新登陸'
]);
case $exception instanceof NotFoundHttpException:
return response()->json([
'code' => 404,
'message' => '路由不存在'
]);
case $exception instanceof \PDOException:
Log::error($exception->getMessage());
return response()->json([
'code' => 500,
'message' => 'sql error'
]);
default:
return response()->json([
'code' => 500,
'message' => $exception->getMessage()
]);
}
}
return parent::render($request, $exception);
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結