Skip to content

Commit 6f9e890

Browse files
Merge pull request #108 from TheDragonCode/1.x
Added `FeedGenerationException` and `FailedFeed` for error handling
2 parents d3f2ce1 + ffbc775 commit 6f9e890

File tree

5 files changed

+120
-7
lines changed

5 files changed

+120
-7
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Exceptions;
6+
7+
use DragonCode\LaravelFeed\Feeds\Feed;
8+
use RuntimeException;
9+
use Throwable;
10+
11+
class FeedGenerationException extends RuntimeException
12+
{
13+
/** @var class-string<Feed> */
14+
public readonly string $feed;
15+
16+
/**
17+
* @param class-string<Feed> $feed
18+
*/
19+
public function __construct(string $feed, Throwable $e)
20+
{
21+
parent::__construct($e->getMessage(), previous: $e);
22+
23+
$this->feed = $feed;
24+
}
25+
26+
/**
27+
* @return class-string<Feed>
28+
*/
29+
public function getFeed(): string
30+
{
31+
return $this->feed;
32+
}
33+
}

src/Services/GeneratorService.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
namespace DragonCode\LaravelFeed\Services;
66

77
use DragonCode\LaravelFeed\Converters\Converter;
8+
use DragonCode\LaravelFeed\Exceptions\FeedGenerationException;
89
use DragonCode\LaravelFeed\Feeds\Feed;
910
use DragonCode\LaravelFeed\Helpers\ConverterHelper;
1011
use DragonCode\LaravelFeed\Queries\FeedQuery;
1112
use Illuminate\Console\OutputStyle;
1213
use Illuminate\Database\Eloquent\Collection;
1314
use Symfony\Component\Console\Helper\ProgressBar;
15+
use Throwable;
1416

1517
use function blank;
1618
use function get_class;
@@ -29,17 +31,25 @@ public function feed(Feed $feed, ?OutputStyle $output = null): void
2931
$file = $this->openFile(
3032
$path = $feed->path()
3133
);
34+
try {
35+
$file = $this->openFile(
36+
$path = $feed->path()
37+
);
3238

33-
$this->performHeader($file, $feed);
34-
$this->performRoot($file, $feed, true);
35-
$this->performInfo($file, $feed);
36-
$this->performRoot($file, $feed, false);
37-
$this->performItem($file, $feed, $output);
38-
$this->performFooter($file, $feed);
39+
$this->performHeader($file, $feed);
40+
$this->performRoot($file, $feed, true);
41+
$this->performInfo($file, $feed);
42+
$this->performRoot($file, $feed, false);
43+
$this->performItem($file, $feed, $output);
44+
$this->performFooter($file, $feed);
3945

40-
$this->release($file, $path);
46+
$this->release($file, $path);
4147

4248
$this->setLastActivity($feed);
49+
$this->setLastActivity($feed);
50+
} catch (Throwable $e) {
51+
throw new FeedGenerationException(get_class($feed), $e);
52+
}
4353
}
4454

4555
protected function performItem($file, Feed $feed, ?OutputStyle $output): void // @pest-ignore-type
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Commands\FeedGenerateCommand;
6+
use DragonCode\LaravelFeed\Exceptions\FeedGenerationException;
7+
use DragonCode\LaravelFeed\Models\Feed;
8+
use Illuminate\Support\Facades\Event;
9+
use Workbench\App\Feeds\FailedFeed;
10+
11+
use function Pest\Laravel\artisan;
12+
13+
test('failed', function () {
14+
Event::fake();
15+
16+
$feed = Feed::create([
17+
'class' => FailedFeed::class,
18+
'title' => 'Failed',
19+
]);
20+
21+
artisan(FeedGenerateCommand::class, ['feed' => $feed->id])
22+
->assertSuccessful()
23+
->run();
24+
})->throws(
25+
exception : FeedGenerationException::class,
26+
exceptionMessage: 'Something went wrong while generating the feed.'
27+
);

workbench/app/Feeds/FailedFeed.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbench\App\Feeds;
6+
7+
use DragonCode\LaravelFeed\Feeds\Feed;
8+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
9+
use Illuminate\Database\Eloquent\Builder;
10+
use Illuminate\Database\Eloquent\Model;
11+
use Workbench\App\Feeds\Items\FailedFeedItem;
12+
use Workbench\App\Models\User;
13+
14+
class FailedFeed extends Feed
15+
{
16+
public function builder(): Builder
17+
{
18+
return User::query();
19+
}
20+
21+
public function item(Model $model): FeedItem
22+
{
23+
return new FailedFeedItem($model);
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbench\App\Feeds\Items;
6+
7+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
8+
use RuntimeException;
9+
10+
class FailedFeedItem extends FeedItem
11+
{
12+
public function toArray(): array
13+
{
14+
throw new RuntimeException(
15+
'Something went wrong while generating the feed.'
16+
);
17+
}
18+
}

0 commit comments

Comments
 (0)