Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot committed Oct 8, 2023
1 parent b54a409 commit 5e6b85c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
26 changes: 13 additions & 13 deletions src/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class Handler extends ExceptionHandler
/**
* Render an exception into an HTTP response.
*
* @param Request $request
* @param Throwable $e
* @param Request $request
* @param Throwable $e
*
* @return Response
* @throws Throwable
*
* @return Response
*/
public function render($request, Throwable $e)
{
Expand All @@ -33,12 +33,12 @@ public function render($request, Throwable $e)
}

return match (true) {
$e instanceof ValidationException => parent::render($request, $e),
$e instanceof ValidationException => parent::render($request, $e),
$e instanceof AuthorizationException => self::throw($e, defaultErrorCode: 9998, responseCode: 403),
$e instanceof NotFoundHttpException => self::throw($e, 9997, 'Route not found!', 404),
$e instanceof QueryException => self::throw($e, 9996, $e->getPrevious()->getMessage(), 500),
$e instanceof NotFoundHttpException => self::throw($e, 9997, 'Route not found!', 404),
$e instanceof QueryException => self::throw($e, 9996, $e->getPrevious()->getMessage(), 500),
$e instanceof ModelNotFoundException => self::throw($e, 9995, $e->getMessage(), 404),
$e instanceof HttpException => $request->wantsJson() ?
$e instanceof HttpException => $request->wantsJson() ?
self::throw($e, defaultErrorCode: 9994) :
parent::render($request, $e),
default => self::throw($e)
Expand All @@ -48,10 +48,10 @@ public function render($request, Throwable $e)
/**
* Convert the given exception to the ValravnException class.
*
* @param Throwable $e
* @param int $defaultErrorCode
* @param string|null $message
* @param int|null $responseCode
* @param Throwable $e
* @param int $defaultErrorCode
* @param string|null $message
* @param int|null $responseCode
*
* @return JsonResponse
*/
Expand All @@ -66,9 +66,9 @@ private static function throw(Throwable $e, int $defaultErrorCode = 9999, string
}

return ValravnException::make(
$message ? : $e->getMessage(),
$message ?: $e->getMessage(),
$errorCode,
$responseCode ? : $e->getCode()
$responseCode ?: $e->getCode()
)
->render();
}
Expand Down
21 changes: 14 additions & 7 deletions tests/Feature/Exceptions/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ protected function setUp(): void
/**
* @test
*
* @return void
* @throws Throwable
*
* @return void
*/
public function rawErrorEnv(): void
{
Expand All @@ -53,8 +54,9 @@ public function rawErrorEnv(): void
/**
* @test
*
* @return void
* @throws Throwable
*
* @return void
*/
public function HttpExceptionMatchExpressionTest(): void
{
Expand All @@ -77,24 +79,29 @@ public function HttpExceptionMatchExpressionTest(): void
/**
* @test
*
* @return void
* @throws Throwable
*
* @return void
*/
public function getErrorCodeFromErrorInstance(): void {
public function getErrorCodeFromErrorInstance(): void
{
$e = ValravnException::make('test exception.', 27);

self::assertJsonStringEqualsJsonString(
'{"title":"Unexpected error!","detail":"test exception.","code":27}',
$this->handler->render(request(), $e)->content()
);
}

/**
* @test
*
* @return void
* @throws Throwable
*
* @return void
*/
public function getCodeFromErrorInstance(): void {
public function getCodeFromErrorInstance(): void
{
$e = new NotFoundHttpException(code: 4040);

self::assertJsonStringEqualsJsonString(
Expand All @@ -106,4 +113,4 @@ public function getCodeFromErrorInstance(): void {
$this->handler->render(request(), $e)->getOriginalContent()['code']
);
}
}
}

0 comments on commit 5e6b85c

Please sign in to comment.