Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling #668

Merged
merged 31 commits into from
Nov 21, 2022
Merged

Improve error handling #668

merged 31 commits into from
Nov 21, 2022

Conversation

bakerkretzmar
Copy link
Collaborator

@bakerkretzmar bakerkretzmar commented Nov 11, 2022

This PR makes some long-overdue improvements to Jigsaw's error handling. The code is mostly copied from Laravel and tweak slightly, and lots of Laravel functionality was removed so that we're only implementing what we absolutely need for now.

Highlights

  • Adds an Artisan-like exception handler to give us more control over error handling.
  • Installs Collision and renders errors with it, so they're now much prettier, show stack traces, and include basic syntax highlighting.
  • Installs Laravel Ignition and maps Blade errors through its custom handler/compiler, so their stack traces display the original uncompiled Blade templates.

Demo

Here are before and after comparisons for a few possible error scenarios:

1. Undefined variable in Blade template

{{ $test }}

Before

Build succeeds (this is bad, it should fail), no error output in the console and no indication that anything is wrong. Additionally, the missing variable is rendered as an error visible in the HTML of the built site:

Warning: Undefined variable $test in /Users/jacob/Code/tighten/tightendotco/cache/914bb3c0cbe5c1750461e191b6c1f4a331a98ef3.php on line 2

After

Build fails with a nice-looking and detailed error message, including a stack trace pointing to the source of the error in the original, uncompiled Blade template:

Screen Shot 2022-11-18 at 12 38 29 PM

2. Invalid expression / syntax error in Blade template

{{ aaaaa ->jnpa af.asdf }}

Before

Build fails with a short, ugly error message (in this case it indicates correctly where the error came from, but I've seen it get that wrong or not provide that info at all many times):

Screen Shot 2022-11-18 at 12 06 40 PM

After

Build fails with a pretty, detailed error message and stack trace:

Screen Shot 2022-11-18 at 12 41 54 PM

3. PHP deprecation

{{ str_contains(null, 'f') }}

Before

Build succeeds, no error messages, and the deprecation warning is rendered into the output of the site:

Deprecated: str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated in /Users/jacob/Code/tighten/tightendotco/cache/5ca2f78ccab5990f8ff2686eb4263c891fd085e1.php on line 11

After

Build succeeds, but displays warnings in the console, and the error is not rendered into the built HTML:

Screen Shot 2022-11-18 at 3 17 28 PM

4. Exception thrown manually

// inside a collection helper in config.php
throw new RuntimeException('Uh oh...');

Before

Screen Shot 2022-11-18 at 11 54 36 AM

After

The original source line of the exception is not visible in this screenshot. This is a small trade-off of this PR—because we're now rendering all exceptions with a lot more detail, some specific cases like this one will no longer display exactly what we need initially. The source line we're looking for here (in config.php) is frame #3, which becomes visible if you re-run the build verbosely (-v). Eventually we could fix this with our own custom exception renderer that shows more stack frames, but that feels like overkill for now.

Screen Shot 2022-11-18 at 12 05 21 PM

@bakerkretzmar bakerkretzmar self-assigned this Nov 11, 2022
Base automatically changed from jbk/service-providers to main November 11, 2022 21:24
github-actions bot and others added 9 commits November 21, 2022 11:17
1) src/Providers/ExceptionServiceProvider.php (phpdoc_to_comment, unary_operator_spaces, not_operator_with_successor_space)
2) src/Bootstrap/HandleExceptions.php (phpdoc_to_comment, unary_operator_spaces, not_operator_with_successor_space)
3) tests/TestCase.php (phpdoc_to_comment, unary_operator_spaces, not_operator_with_successor_space)
1) src/Exceptions/Handler.php (trailing_comma_in_multiline, unary_operator_spaces, not_operator_with_successor_space)
@bakerkretzmar bakerkretzmar marked this pull request as ready for review November 21, 2022 16:20
@damiani
Copy link
Contributor

damiani commented Nov 21, 2022

Love this! 🎉

@mattstauffer
Copy link
Member

WHOA. Love this!

@bakerkretzmar bakerkretzmar merged commit 30a77b2 into main Nov 21, 2022
@bakerkretzmar bakerkretzmar deleted the jbk/exception-handling branch November 21, 2022 19:31
@husseinalhammad
Copy link
Contributor

Awesome stuff 🚀

simonhamp added a commit to simonhamp/laravel-mix-jigsaw that referenced this pull request Dec 8, 2022
Lets the build's own errors (nicely output from tighten/jigsaw#668) bubble up to the console when using `npm run watch`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants