Skip to content

Commit

Permalink
fix: tests on exception
Browse files Browse the repository at this point in the history
  • Loading branch information
matthv committed Nov 5, 2024
1 parent 8d14ba9 commit c2d972e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
12 changes: 6 additions & 6 deletions packages/Agent/src/Http/Traits/ErrorHandling.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public function getErrorStatus(Throwable $error): int

public function getErrorMessage(Throwable $error): string
{
if ($error instanceof HttpException ||
is_subclass_of($error, HttpException::class) ||
$error instanceof ForestException) {
return $error->getMessage();
}

if ($customizer = AgentFactory::get('customizeErrorMessage')) {
$message = $customizer($error);
if ($message) {
return $message;
}
}

if ($error instanceof HttpException ||
is_subclass_of($error, HttpException::class) ||
$error instanceof ForestException) {
return $error->getMessage();
}

return 'Unexpected error';
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Agent/Http/ForestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use ForestAdmin\AgentPHP\Agent\Http\ForestController;
use ForestAdmin\AgentPHP\Agent\Http\Request;
use ForestAdmin\AgentPHP\DatasourceToolkit\Datasource;
use ForestAdmin\AgentPHP\DatasourceToolkit\Exceptions\ForestException;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -59,7 +58,7 @@
->getMock();
$forestControllerMock->expects($this->once())
->method('getClosure')
->willReturn(fn () => throw new ForestException());
->willReturn(fn () => throw new \Exception());

$result = $forestControllerMock->__invoke($request);

Expand All @@ -72,7 +71,7 @@
[
'errors' => [
[
'name' => 'ForestException',
'name' => 'Exception',
'detail' => 'Unexpected error',
'status' => 500,
],
Expand Down
2 changes: 1 addition & 1 deletion tests/Agent/Http/Traits/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@
$this->buildAgent(new Datasource());
$trait = $this->getObjectForTrait(ErrorHandling::class);

expect($trait->getErrorMessage(new ForestException('test')))->toEqual('Unexpected error');
expect($trait->getErrorMessage(new \Exception('test')))->toEqual('Unexpected error');
});

0 comments on commit c2d972e

Please sign in to comment.