Skip to content

Commit 3233336

Browse files
Merge pull request #267 from laravel/quieter-errors
Reduce error popups
2 parents c7e41ff + 2fe5658 commit 3233336

File tree

5 files changed

+103
-38
lines changed

5 files changed

+103
-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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,18 @@ protected function fromFile($file, $path, $namespace)
100100
protected function linesFromJsonFile($file)
101101
{
102102
$contents = file_get_contents($file);
103+
104+
try {
105+
$json = json_decode($contents, true) ?? [];
106+
} catch (\Throwable $e) {
107+
return [[], []];
108+
}
109+
110+
if (count($json) === 0) {
111+
return [[], []];
112+
}
113+
103114
$lines = explode(PHP_EOL, $contents);
104-
$json = json_decode($contents, true);
105115
$encoded = array_map(
106116
fn($k) => [json_encode($k), $k],
107117
array_keys($json),
@@ -110,7 +120,7 @@ protected function linesFromJsonFile($file)
110120
$searchRange = 2;
111121

112122
foreach ($encoded as $index => $keys) {
113-
// 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
114124
if (strpos($lines[$index + 1] ?? '', $keys[0]) !== false) {
115125
$result[$keys[1]] = $index + 2;
116126
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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,18 @@ $translator = new class
100100
protected function linesFromJsonFile($file)
101101
{
102102
$contents = file_get_contents($file);
103+
104+
try {
105+
$json = json_decode($contents, true) ?? [];
106+
} catch (\\Throwable $e) {
107+
return [[], []];
108+
}
109+
110+
if (count($json) === 0) {
111+
return [[], []];
112+
}
113+
103114
$lines = explode(PHP_EOL, $contents);
104-
$json = json_decode($contents, true);
105115
$encoded = array_map(
106116
fn($k) => [json_encode($k), $k],
107117
array_keys($json),
@@ -110,7 +120,7 @@ $translator = new class
110120
$searchRange = 2;
111121
112122
foreach ($encoded as $index => $keys) {
113-
// 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
114124
if (strpos($lines[$index + 1] ?? '', $keys[0]) !== false) {
115125
$result[$keys[1]] = $index + 2;
116126
continue;

0 commit comments

Comments
 (0)