Skip to content

Commit 555c261

Browse files
committed
Error handling improvements
ApiGatewayException handling now checks if the request expects JSON and aborts only for standard web requests
1 parent af42cee commit 555c261

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## Unreleased
99
### Added
1010
- Query builder with `where()->get()` support for remote models.
11+
- Smart error handling based on the request's expected format.
12+
- Error propagation improvements with tests for all status codes.
1113

1214
## [0.4.1] - 2025-07-10
1315
### Added

src/Providers/MicroserviceServiceProvider.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ public function boot(Router $router): void
214214

215215
if (method_exists($handler, 'renderable')) {
216216
$handler->renderable(function (ApiGatewayException $e, $request) {
217-
return response()->json($e->getData(), $e->getStatusCode());
217+
$status = $e->getStatusCode();
218+
$message = $e->getMessage();
219+
220+
if (! $request->expectsJson()) {
221+
abort($status, $message);
222+
}
223+
224+
return response()->json(['error' => $message], $status);
218225
});
219226
}
220227
}

src/Services/ApiGatewayClient.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,22 @@ protected function handleResponse($response)
6969
if (is_object($response) && method_exists($response, 'failed') && $response->failed()) {
7070
$data = method_exists($response, 'json') ? $response->json() : [];
7171

72+
$message = '';
73+
if (is_array($data)) {
74+
$message = $data['message'] ?? ($data['error'] ?? '');
75+
}
76+
7277
throw new ApiGatewayException(
7378
method_exists($response, 'status') ? $response->status() : 500,
74-
is_array($data) ? $data : []
79+
is_array($data) ? $data : [],
80+
$message
7581
);
7682
}
7783

84+
if (is_object($response) && method_exists($response, 'json')) {
85+
return response()->json($response->json(), $response->status());
86+
}
87+
7888
return $response;
7989
}
8090
}

tests/Services/ApiGatewayErrorTest.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,52 @@ protected function getPackageProviders($app)
1818
protected function setUp(): void
1919
{
2020
parent::setUp();
21-
Http::fake(['*' => Http::response(['error' => 'unavailable'], 503)]);
2221

2322
Route::get('/gateway-error', function () {
2423
return app(ApiGatewayClient::class)->get('/fail');
2524
});
2625
}
2726

27+
public static function statusProvider(): array
28+
{
29+
return [[200], [201], [400], [401], [419], [500]];
30+
}
31+
32+
/**
33+
* @test
34+
* @dataProvider statusProvider
35+
*/
36+
public function propagates_gateway_status_code(int $status)
37+
{
38+
Http::fake(['*' => Http::response(['message' => 'm'], $status)]);
39+
40+
$response = $this->getJson('/gateway-error');
41+
$response->assertStatus($status);
42+
43+
if ($status >= 400) {
44+
$response->assertJson(['error' => 'm']);
45+
}
46+
}
47+
2848
/** @test */
29-
public function propagates_gateway_status_code()
49+
public function aborts_in_frontend_context()
3050
{
31-
$this->get('/gateway-error')
32-
->assertStatus(503)
33-
->assertJson(['error' => 'unavailable']);
51+
Http::fake(['*' => Http::response(['message' => 'nope'], 401)]);
52+
53+
$this->withoutExceptionHandling();
54+
$this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
55+
$this->expectExceptionMessage('nope');
56+
57+
$this->get('/gateway-error');
58+
}
59+
60+
/** @test */
61+
public function returns_json_in_service_context()
62+
{
63+
Http::fake(['*' => Http::response(['message' => 'bad'], 400)]);
64+
65+
$this->getJson('/gateway-error')
66+
->assertStatus(400)
67+
->assertJson(['error' => 'bad']);
3468
}
3569
}

0 commit comments

Comments
 (0)