Skip to content

Commit

Permalink
Add specify exceptions for exceptions handling the vite manifest file (
Browse files Browse the repository at this point in the history
…#52169)

* Add specify exceptions for exceptions handling the manifest files

* Fix CS

* Change to single ViteException
  • Loading branch information
SamuelWei authored Jul 18, 2024
1 parent 1883126 commit 6c16f47
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Illuminate\Foundation;

use Exception;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
Expand Down Expand Up @@ -682,7 +681,7 @@ public function asset($asset, $buildDirectory = null)
* @param string|null $buildDirectory
* @return string
*
* @throws \Exception
* @throws \Illuminate\Foundation\ViteException
*/
public function content($asset, $buildDirectory = null)
{
Expand All @@ -693,7 +692,7 @@ public function content($asset, $buildDirectory = null)
$path = public_path($buildDirectory.'/'.$chunk['file']);

if (! is_file($path) || ! file_exists($path)) {
throw new Exception("Unable to locate file from Vite manifest: {$path}.");
throw new ViteException("Unable to locate file from Vite manifest: {$path}.");
}

return file_get_contents($path);
Expand Down Expand Up @@ -773,12 +772,12 @@ public function manifestHash($buildDirectory = null)
* @param string $file
* @return array
*
* @throws \Exception
* @throws \Illuminate\Foundation\ViteException
*/
protected function chunk($manifest, $file)
{
if (! isset($manifest[$file])) {
throw new Exception("Unable to locate file in Vite manifest: {$file}.");
throw new ViteException("Unable to locate file in Vite manifest: {$file}.");
}

return $manifest[$file];
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Foundation/ViteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Foundation;

use Exception;

class ViteException extends Exception
{
//
}
7 changes: 4 additions & 3 deletions src/Illuminate/Foundation/ViteManifestNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Illuminate\Foundation;

use Exception;

class ViteManifestNotFoundException extends Exception
/**
* @deprecated use ViteException
*/
class ViteManifestNotFoundException extends ViteException
{
//
}
17 changes: 13 additions & 4 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Illuminate\Tests\Foundation;

use Exception;
use Illuminate\Foundation\Vite;
use Illuminate\Foundation\ViteException;
use Illuminate\Foundation\ViteManifestNotFoundException;
use Illuminate\Support\Facades\Vite as ViteFacade;
use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase;
Expand Down Expand Up @@ -564,7 +565,15 @@ public function testItCanGenerateIndividualAssetUrlInHotMode()

public function testItThrowsWhenUnableToFindAssetManifestInBuildMode()
{
$this->expectException(Exception::class);
$this->expectException(ViteException::class);
$this->expectExceptionMessage('Vite manifest not found at: '.public_path('build/manifest.json'));

ViteFacade::asset('resources/js/app.js');
}

public function testItThrowsDeprecatedExecptionWhenUnableToFindAssetManifestInBuildMode()
{
$this->expectException(ViteManifestNotFoundException::class);
$this->expectExceptionMessage('Vite manifest not found at: '.public_path('build/manifest.json'));

ViteFacade::asset('resources/js/app.js');
Expand All @@ -574,7 +583,7 @@ public function testItThrowsWhenUnableToFindAssetChunkInBuildMode()
{
$this->makeViteManifest();

$this->expectException(Exception::class);
$this->expectException(ViteException::class);
$this->expectExceptionMessage('Unable to locate file in Vite manifest: resources/js/missing.js');

ViteFacade::asset('resources/js/missing.js');
Expand Down Expand Up @@ -1232,7 +1241,7 @@ public function testItThrowsWhenUnableToFindFileToRetrieveContent()
{
$this->makeViteManifest();

$this->expectException(Exception::class);
$this->expectException(ViteException::class);
$this->expectExceptionMessage('Unable to locate file from Vite manifest: '.public_path('build/assets/app.versioned.js'));

ViteFacade::content('resources/js/app.js');
Expand Down

0 comments on commit 6c16f47

Please sign in to comment.