Skip to content

Commit 61d6bab

Browse files
committed
fix: return from render method for renderable exception
1 parent a63fa71 commit 61d6bab

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/ExceptionHandler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace KodePandai\ApiResponse;
44

5+
use Illuminate\Contracts\Support\Renderable;
56
use Illuminate\Http\Response;
67
use Illuminate\Support\Facades\App;
78
use Symfony\Component\HttpKernel\Exception\HttpException;
@@ -11,9 +12,15 @@ class ExceptionHandler
1112
{
1213
/**
1314
* Render throwable as ApiResponse
15+
*
16+
* @return ApiResponse|Response
1417
*/
15-
public static function renderAsApiResponse(Throwable $e): ApiResponse
18+
public static function renderAsApiResponse(Throwable $e)
1619
{
20+
if ($e instanceof Renderable) {
21+
return $e->render();
22+
}
23+
1724
$self = new self;
1825

1926
$traces = $self->getTraces($e);

tests/ExceptionHandlerTest.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
use Illuminate\Http\Response;
44
use Illuminate\Support\Facades\App;
55
use Illuminate\Support\Facades\Route;
6-
use KodePandai\ApiResponse\Exceptions\ApiException;
7-
use KodePandai\ApiResponse\Exceptions\ApiValidationException;
86
use KodePandai\ApiResponse\Tests\TestCase;
97
use function Pest\Laravel\getJson;
108

11-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12-
139
uses(TestCase::class);
1410

1511
it('render an api response when exception is thrown', function () {
1612
//.
17-
Route::get('error', fn () => throw new InvalidArgumentException('Hehehe'));
13+
Route::get('error', function () {
14+
throw new InvalidArgumentException('Hehehe');
15+
});
1816

1917
getJson('error')
2018
->assertStatus(Response::HTTP_INTERNAL_SERVER_ERROR)
@@ -26,7 +24,9 @@
2624
it('does not display message and stack traces on production', function () {
2725
//.
2826
Route::get('error', function () {
29-
App::detectEnvironment(fn () => 'production');
27+
App::detectEnvironment(function () {
28+
return 'production';
29+
});
3030

3131
throw new InvalidArgumentException('Hihihi');
3232
});
@@ -49,11 +49,21 @@
4949

5050
it('only display traces when response status code in [400, 502, 500]', function () {
5151
//.
52-
Route::get('400', fn () => throw new ApiException());
53-
Route::get('502', fn () => throw new ApiException(502, 502));
54-
Route::get('500', fn () => throw new ApiException(500, 500));
55-
Route::get('404', fn () => throw new NotFoundHttpException(404));
56-
Route::get('422', fn () => throw new ApiValidationException());
52+
Route::get('400', function () {
53+
return abort(Response::HTTP_BAD_REQUEST);
54+
});
55+
Route::get('502', function () {
56+
return abort(Response::HTTP_BAD_GATEWAY);
57+
});
58+
Route::get('500', function () {
59+
return abort(Response::HTTP_INTERNAL_SERVER_ERROR);
60+
});
61+
Route::get('404', function () {
62+
return abort(Response::HTTP_NOT_FOUND);
63+
});
64+
Route::get('422', function () {
65+
throw abort(Response::HTTP_UNPROCESSABLE_ENTITY);
66+
});
5767

5868
getJson('400')
5969
->assertStatus(Response::HTTP_BAD_REQUEST)

0 commit comments

Comments
 (0)