Skip to content

Commit

Permalink
feat: support ignore exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Oct 17, 2023
1 parent 4c05fdc commit e82d955
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Support/Traits/ExceptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ protected function prepareJsonResponse($request, $e)
{
// 要求请求头 header 中包含 /json 或 +json,如:Accept:application/json
// 或者是 ajax 请求,header 中包含 X-Requested-With:XMLHttpRequest;
$exceptionConfig = Arr::get(Config::get('response.exception'), get_class($e));
$exceptionConfig = Config::get('response.exception.'.get_class($e));

if (!is_null($exceptionConfig)) return parent::prepareJsonResponse($request, $e);

/** @var \Illuminate\Foundation\Exceptions\Handler $this */
$isHttpException = $this->isHttpException($e);
Expand Down Expand Up @@ -79,11 +81,13 @@ protected function buildFailedValidationResponse(Request $request, array $errors
*/
protected function invalidJson($request, ValidationException $exception)
{
return Response::fail(
$exceptionConfig = Config::get('response.exception.'.ValidationException::class);

return !is_null($exceptionConfig) ? Response::fail(
$exception->validator->errors()->first(),
Arr::get(Config::get('response.exception'), ValidationException::class.'.code', 422),
Arr::get($exceptionConfig, 'code', 422),
$exception->errors()
);
) : parent::invalidJson($request, $exception);
}

/**
Expand All @@ -94,10 +98,10 @@ protected function invalidJson($request, ValidationException $exception)
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
$exceptionConfig = Arr::get(Config::get('response.exception'), AuthenticationException::class);
$exceptionConfig = Config::get('response.exception.'.AuthenticationException::class);

return $request->expectsJson()
return $exceptionConfig && $request->expectsJson()
? Response::errorUnauthorized($exceptionConfig['message'] ?? $exception->getMessage())
: redirect()->guest($exception->redirectTo() ?? route('login'));
: parent::unauthenticated($request, $exception);
}
}

0 comments on commit e82d955

Please sign in to comment.