Skip to content

Commit 2fe5658

Browse files
committed
quiet app startup errors
1 parent b7e1fd4 commit 2fe5658

File tree

5 files changed

+83
-38
lines changed

5 files changed

+83
-38
lines changed

php-templates/bootstrap-laravel.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,35 @@
55
define('LARAVEL_START', microtime(true));
66

77
require_once __DIR__ . '/../autoload.php';
8-
$app = require_once __DIR__ . '/../../bootstrap/app.php';
8+
9+
class LaravelVsCode
10+
{
11+
public static function relativePath($path)
12+
{
13+
if (!str_contains($path, base_path())) {
14+
return (string) $path;
15+
}
16+
17+
return ltrim(str_replace(base_path(), '', realpath($path)), DIRECTORY_SEPARATOR);
18+
}
19+
20+
public static function outputMarker($key)
21+
{
22+
return '__VSCODE_LARAVEL_' . $key . '__';
23+
}
24+
25+
public static function startupError(\Throwable $e)
26+
{
27+
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
28+
}
29+
}
30+
31+
try {
32+
$app = require_once __DIR__ . '/../../bootstrap/app.php';
33+
} catch (\Throwable $e) {
34+
LaravelVsCode::startupError($e);
35+
exit(1);
36+
}
937

1038
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
1139
{
@@ -21,23 +49,16 @@ public function boot()
2149
}
2250
});
2351

24-
class LaravelVsCode
25-
{
26-
public static function relativePath($path)
27-
{
28-
if (!str_contains($path, base_path())) {
29-
return (string) $path;
30-
}
31-
32-
return ltrim(str_replace(base_path(), '', realpath($path)), DIRECTORY_SEPARATOR);
33-
}
52+
try {
53+
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
54+
$kernel->bootstrap();
55+
} catch (\Throwable $e) {
56+
LaravelVsCode::startupError($e);
57+
exit(1);
3458
}
3559

36-
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
37-
$kernel->bootstrap();
38-
39-
echo '__VSCODE_LARAVEL_START_OUTPUT__';
60+
echo LaravelVsCode::outputMarker('START_OUTPUT');
4061
__VSCODE_LARAVEL_OUTPUT__;
41-
echo '__VSCODE_LARAVEL_END_OUTPUT__';
62+
echo LaravelVsCode::outputMarker('END_OUTPUT');
4263

4364
exit(0);

php-templates/translations.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected function linesFromJsonFile($file)
103103

104104
try {
105105
$json = json_decode($contents, true) ?? [];
106-
} catch (Exception $e) {
106+
} catch (\Throwable $e) {
107107
return [[], []];
108108
}
109109

@@ -120,7 +120,7 @@ protected function linesFromJsonFile($file)
120120
$searchRange = 2;
121121

122122
foreach ($encoded as $index => $keys) {
123-
// Pretty likely the be on the line that is the index, go happy path first
123+
// Pretty likely to be on the line that is the index, go happy path first
124124
if (strpos($lines[$index + 1] ?? '', $keys[0]) !== false) {
125125
$result[$keys[1]] = $index + 2;
126126
continue;

src/support/php.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,11 @@ export const runInLaravel = <T>(
242242
throw new Error(result);
243243
})
244244
.catch((e) => {
245-
if (e.toString().includes("ParseError")) {
246-
// If we it's a parse error let's not show the popup,
245+
if (
246+
e.toString().includes("ParseError") ||
247+
e.toString().includes(toTemplateVar("STARTUP_ERROR"))
248+
) {
249+
// If we it's a parse error or an app-wide error let's not show the popup,
247250
// probably just a momentary syntax error
248251
error(e);
249252
return;

src/templates/bootstrap-laravel.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,35 @@ error_reporting(E_ERROR | E_PARSE);
55
define('LARAVEL_START', microtime(true));
66
77
require_once __DIR__ . '/../autoload.php';
8-
$app = require_once __DIR__ . '/../../bootstrap/app.php';
8+
9+
class LaravelVsCode
10+
{
11+
public static function relativePath($path)
12+
{
13+
if (!str_contains($path, base_path())) {
14+
return (string) $path;
15+
}
16+
17+
return ltrim(str_replace(base_path(), '', realpath($path)), DIRECTORY_SEPARATOR);
18+
}
19+
20+
public static function outputMarker($key)
21+
{
22+
return '__VSCODE_LARAVEL_' . $key . '__';
23+
}
24+
25+
public static function startupError(\\Throwable $e)
26+
{
27+
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
28+
}
29+
}
30+
31+
try {
32+
$app = require_once __DIR__ . '/../../bootstrap/app.php';
33+
} catch (\\Throwable $e) {
34+
LaravelVsCode::startupError($e);
35+
exit(1);
36+
}
937
1038
$app->register(new class($app) extends \\Illuminate\\Support\\ServiceProvider
1139
{
@@ -21,24 +49,17 @@ $app->register(new class($app) extends \\Illuminate\\Support\\ServiceProvider
2149
}
2250
});
2351
24-
class LaravelVsCode
25-
{
26-
public static function relativePath($path)
27-
{
28-
if (!str_contains($path, base_path())) {
29-
return (string) $path;
30-
}
31-
32-
return ltrim(str_replace(base_path(), '', realpath($path)), DIRECTORY_SEPARATOR);
33-
}
52+
try {
53+
$kernel = $app->make(Illuminate\\Contracts\\Console\\Kernel::class);
54+
$kernel->bootstrap();
55+
} catch (\\Throwable $e) {
56+
LaravelVsCode::startupError($e);
57+
exit(1);
3458
}
3559
36-
$kernel = $app->make(Illuminate\\Contracts\\Console\\Kernel::class);
37-
$kernel->bootstrap();
38-
39-
echo '__VSCODE_LARAVEL_START_OUTPUT__';
60+
echo LaravelVsCode::outputMarker('START_OUTPUT');
4061
__VSCODE_LARAVEL_OUTPUT__;
41-
echo '__VSCODE_LARAVEL_END_OUTPUT__';
62+
echo LaravelVsCode::outputMarker('END_OUTPUT');
4263
4364
exit(0);
4465
`;

src/templates/translations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ $translator = new class
103103
104104
try {
105105
$json = json_decode($contents, true) ?? [];
106-
} catch (Exception $e) {
106+
} catch (\\Throwable $e) {
107107
return [[], []];
108108
}
109109
@@ -120,7 +120,7 @@ $translator = new class
120120
$searchRange = 2;
121121
122122
foreach ($encoded as $index => $keys) {
123-
// Pretty likely the be on the line that is the index, go happy path first
123+
// Pretty likely to be on the line that is the index, go happy path first
124124
if (strpos($lines[$index + 1] ?? '', $keys[0]) !== false) {
125125
$result[$keys[1]] = $index + 2;
126126
continue;

0 commit comments

Comments
 (0)