Skip to content

Commit 32c5354

Browse files
committed
Paginate error handling
Added error handling in Model::paginate() so a 404 from the gateway now returns an empty paginator instead of throwing an exception
1 parent 4989c77 commit 32c5354

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
### Fixed
10+
- `paginate()` returns an empty paginator with a 200 status when the gateway responds with 404.
11+
12+
813
## [0.4.2] - 2025-07-21
914
### Added
1015
- Query builder with `where()->get()` support for remote models.

src/Models/Model.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Collection;
99
use Illuminate\Support\Str;
1010
use Kroderdev\LaravelMicroserviceCore\Contracts\ApiGatewayClientInterface;
11+
use Kroderdev\LaravelMicroserviceCore\Exceptions\ApiGatewayException;
1112
use Kroderdev\LaravelMicroserviceCore\Interfaces\ApiModelContract;
1213
use Kroderdev\LaravelMicroserviceCore\Traits\ApiModelTrait;
1314
use Kroderdev\LaravelMicroserviceCore\Traits\ParsesApiResponse;
@@ -99,8 +100,18 @@ public static function paginate($perPage = 15, $columns = ['*'], $pageName = 'pa
99100
{
100101
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
101102
$query = [$pageName => $page, 'per_page' => $perPage];
102-
$response = static::client()->get(static::endpoint(), $query);
103-
$data = static::parseResponse($response);
103+
104+
// Handle the case where the API returns a 404 for an empty collection
105+
try {
106+
$response = static::client()->get(static::endpoint(), $query);
107+
$data = static::parseResponse($response);
108+
} catch (ApiGatewayException $e) {
109+
if ($e->getStatusCode() === 404) {
110+
return new LengthAwarePaginator([], 0, $perPage);
111+
}
112+
113+
throw $e;
114+
}
104115

105116
return static::fromPaginatedResponse($data);
106117
}

tests/Models/ApiModelTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,28 @@ public function from_api_response_maps_nested_relations()
386386
$this->assertEquals('file3.txt', $deep->children[0]->name);
387387
$this->assertEquals(789, $deep->children[0]->size);
388388
}
389+
390+
/** @test */
391+
public function paginate_returns_empty_paginator_on_404()
392+
{
393+
$this->gateway = new class () extends FakeGatewayClient {
394+
public function get(string $uri, array $query = [])
395+
{
396+
parent::get($uri, $query);
397+
398+
throw new \Kroderdev\LaravelMicroserviceCore\Exceptions\ApiGatewayException(404);
399+
}
400+
};
401+
$this->app->bind(ApiGatewayClientInterface::class, fn () => $this->gateway);
402+
403+
$paginator = RemoteUser::paginate(10);
404+
405+
$this->assertSame([
406+
['method' => 'GET', 'uri' => '/users', 'query' => ['page' => 1, 'per_page' => 10]],
407+
], $this->gateway->getCalls());
408+
$this->assertInstanceOf(\Illuminate\Pagination\LengthAwarePaginator::class, $paginator);
409+
$this->assertCount(0, $paginator->items());
410+
$this->assertEquals(0, $paginator->total());
411+
$this->assertEquals(200, response()->json($paginator->items())->status());
412+
}
389413
}

0 commit comments

Comments
 (0)