Skip to content

Commit

Permalink
优化异常处理json的判断
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Aug 13, 2024
1 parent f574a5f commit 6259165
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/think/exception/Handle.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class Handle
ValidateException::class,
];

protected $isJson = false;

public function __construct(protected App $app)
{
}
Expand All @@ -59,13 +57,13 @@ public function report(Throwable $exception): void
'message' => $this->getMessage($exception),
'code' => $this->getCode($exception),
];
$log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
$log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
} else {
$data = [
'code' => $this->getCode($exception),
'message' => $this->getMessage($exception),
];
$log = "[{$data['code']}]{$data['message']}";
$log = "[{$data['code']}]{$data['message']}";
}

if ($this->app->config->get('log.record_trace')) {
Expand All @@ -74,7 +72,8 @@ public function report(Throwable $exception): void

try {
$this->app->log->record($log, 'error');
} catch (Exception $e) {}
} catch (Exception $e) {
}
}
}

Expand All @@ -99,13 +98,12 @@ protected function isIgnoreReport(Throwable $exception): bool
*/
public function render(Request $request, Throwable $e): Response
{
$this->isJson = $request->isJson();
if ($e instanceof HttpResponseException) {
return $e->getResponse();
} elseif ($e instanceof HttpException) {
return $this->renderHttpException($e);
return $this->renderHttpException($request, $e);
} else {
return $this->convertExceptionToResponse($e);
return $this->convertExceptionToResponse($request, $e);
}
}

Expand All @@ -128,15 +126,15 @@ public function renderForConsole(Output $output, Throwable $e): void
* @param HttpException $e
* @return Response
*/
protected function renderHttpException(HttpException $e): Response
protected function renderHttpException(Request $request, HttpException $e): Response
{
$status = $e->getStatusCode();
$template = $this->app->config->get('app.http_exception_template');

if (!$this->app->isDebug() && !empty($template[$status])) {
return Response::create($template[$status], 'view', $status)->assign(['e' => $e]);
} else {
return $this->convertExceptionToResponse($e);
return $this->convertExceptionToResponse($request, $e);
}
}

Expand Down Expand Up @@ -214,17 +212,22 @@ protected function getDebugMsg(Throwable $exception): array
];
}

protected function isJson(Request $request, Throwable $exception)
{
return $request->isJson();
}

/**
* @access protected
* @param Throwable $exception
* @return Response
*/
protected function convertExceptionToResponse(Throwable $exception): Response
protected function convertExceptionToResponse(Request $request, Throwable $exception): Response
{
if (!$this->isJson) {
$response = Response::create($this->renderExceptionContent($exception));
} else {
if ($this->isJson($request, $exception)) {
$response = Response::create($this->convertExceptionToArray($exception), 'json');
} else {
$response = Response::create($this->renderExceptionContent($exception));
}

if ($exception instanceof HttpException) {
Expand Down

0 comments on commit 6259165

Please sign in to comment.