reportable(function (Throwable $e) { // }); } /** * 将异常转换为 HTTP 响应。 * @param $request * @param Throwable $e * @return array|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response * @throws Throwable */ public function render($request, Throwable $e) { if ($e instanceof ApiException) { return response()->json(Base::retError($e->getMessage(), $e->getData(), $e->getCode())); } elseif ($e instanceof ModelNotFoundException) { return response()->json(Base::retError('Interface error')); } return parent::render($request, $e); } /** * 重写report优雅记录 * @param Throwable $e * @throws Throwable */ public function report(Throwable $e) { if ($e instanceof ApiException) { if ($e->getCode() !== -1) { Log::error($e->getMessage(), [ 'code' => $e->getCode(), 'data' => $e->getData(), 'exception' => ' at ' . $e->getFile() . ':' . $e->getLine() ]); } } else { parent::report($e); } } }