-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Improved Alpine error resiliency and logging #2027
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
Merged
calebporzio
merged 17 commits into
alpinejs:main
from
blackmagic0:error-handling-feature
Oct 11, 2021
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
448dccd
Iteration on error logging, adding event on errors for plugins to lis…
6afdc79
Iteration on error logging, adding event on errors for plugins to lis…
f26f972
Iteration on error logging, adding event on errors for plugins to lis…
991f2b3
move el and expression onto detail object directly
danddanddand 28d7484
add back error for better dev experience
danddanddand 084c9e7
Merge branch 'alpinejs:main' into v2-error-handling
e18ec7f
Merge remote-tracking branch 'origin/main' into v2-error-handling
647cec1
fix two bad tests that fail when exception handling is working
danddanddand ecc0f2d
Merge branch 'alpinejs:main' into v2-error-handling
c0fe6a9
Merge branch 'main' into error-handling-feature
af77e61
remove test since empty x-init is not valid
danddanddand d256db2
remove error event dispatching
danddanddand 252f6d4
remove unnecessary try/catch that should be handled internally
danddanddand 8cfa4ec
Fixed extra tab
13da65b
Removed unused import
77ab0b2
prevent useless 'func is not a function' errors
danddanddand 4af4595
Merge branch 'main' into error-handling-feature
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| export function tryCatch(el, expression, callback, ...args) { | ||
| try { | ||
| return callback(...args) | ||
| } catch (e) { | ||
| handleError( e, el, expression ) | ||
| } | ||
| } | ||
|
|
||
| export function handleError(error, el, expression = undefined) { | ||
| Object.assign( error, { el, expression } ) | ||
|
|
||
| console.warn(`Alpine Expression Error: ${error.message}\n\n${ expression ? 'Expression: \"' + expression + '\"\n\n' : '' }`, el) | ||
|
|
||
| setTimeout( () => { throw error }, 0 ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| import { haveText, html, test } from '../utils' | ||
|
|
||
| export function setupConsoleInterceptor( ...targetIds ) { | ||
| const mappedTargetIds = targetIds.map( tid => `'${tid}'` ).join( ',' ) | ||
| return ` | ||
| let errorContainer = document.createElement('div'); | ||
| errorContainer.id = 'errors' | ||
| errorContainer.textContent = 'false' | ||
| document.querySelector('#root').after(errorContainer) | ||
| console.warnlog = console.warn.bind(console) | ||
| console.warn = function () { | ||
| document.getElementById( 'errors' ).textContent = [${mappedTargetIds}].some( target => arguments[1] === document.getElementById( target ) ) | ||
| console.warnlog.apply(console, arguments) | ||
| } | ||
| ` | ||
| } | ||
|
|
||
| export function assertConsoleInterceptorHadErrorWithCorrectElement() { | ||
| return ({get}) => { | ||
| get('#errors').should(haveText('true')) | ||
| }; | ||
| } | ||
|
|
||
| test('x-for identifier issue', | ||
| [html` | ||
| <div x-data="{ items: ['foo'] }"> | ||
| <template id="xfor" x-for="item in itemzzzz"> | ||
| <span x-text="item"></span> | ||
| </template> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xfor" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('x-text identifier issue', | ||
| [html` | ||
| <div x-data="{ items: ['foo'] }"> | ||
| <template x-for="item in items"> | ||
| <span id="xtext" x-text="itemzzz"></span> | ||
| </template> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xtext" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('x-init identifier issue', | ||
| [html` | ||
| <div id="xinit" x-data x-init="doesNotExist()"> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xinit" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('x-show identifier issue', | ||
| [html` | ||
| <div id="xshow" x-data="{isOpen: true}" x-show="isVisible"> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xshow" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('x-bind class object syntax identifier issue', | ||
| [html` | ||
| <div x-data="{isOpen: true}"> | ||
| <div id="xbind" :class="{ 'block' : isVisible, 'hidden' : !isVisible }"></div> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xbind" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('x-model identifier issue', | ||
| [html` | ||
| <div x-data="{value: ''}"> | ||
| <input id="xmodel" x-model="thething"/> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xmodel" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('x-if identifier issue', | ||
| [html` | ||
| <div x-data="{value: ''}"> | ||
| <template id="xif" x-if="valuez === ''"> | ||
| <span>Words</span> | ||
| </template> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xif" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('x-if identifier issue ( function )', | ||
| [html` | ||
| <div x-data="{shouldOpen: function(){}}"> | ||
| <template id="xif" x-if="isOpen()"> | ||
| <span>Words</span> | ||
| </template> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xif" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('x-effect identifier issue', | ||
| [html` | ||
| <div id="xeffect" x-data="{ label: 'Hello' }" x-effect="System.out.println(label)"> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xeffect" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('x-on identifier issue', | ||
| [html` | ||
| <div x-data="{ label: 'Hello' }"> | ||
| <div x-text="label"></div> | ||
| <button id="xon" x-on:click="labelz += ' World!'">Change Message</button> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xon" ) | ||
| ], | ||
| ({ get }) => { | ||
| get( "#xon" ).click() | ||
| get( "#errors" ).should(haveText('true')) | ||
| }, | ||
| true | ||
| ) | ||
|
|
||
| test('x-data syntax error', | ||
| [html` | ||
| <div id="xdata" x-data="{ label: 'Hello' }aaa"> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xdata" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('if statement syntax error', | ||
| [html` | ||
| <div x-data="{ label: 'Hello' }"> | ||
| <div id="xtext" x-text="if( false { label } else { 'bye' }"></div> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xtext" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('x-data with reference error and multiple errors', | ||
| [html` | ||
| <div id="xdata" x-data="{ items : [ {v:'one'},{v:'two'}], replaceItems }"> | ||
| <template id="xtext" x-for="item in items"> | ||
| <span x-text="item.v"></span> | ||
| </template> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xdata", "xtext" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) | ||
|
|
||
| test('evaluation with syntax error', | ||
| [html` | ||
| <div x-data="{value: ''}"> | ||
| <template id="xif" x-if="value ==== ''"> | ||
| <span>Words</span> | ||
| </template> | ||
| </div> | ||
| `, | ||
| setupConsoleInterceptor( "xif" ) | ||
| ], | ||
| assertConsoleInterceptorHadErrorWithCorrectElement(), | ||
| true | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,7 +70,7 @@ test('store\'s "this" context is reactive for init function', | |
| [html` | ||
| <div x-data> | ||
| <span x-text="$store.test.count"></span> | ||
| <button @click="$store.test.increment()" id="button">increment</button> | ||
| <button id="button">increment</button> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose for removing the @click here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. increment function doesn't exist so test fails with reference error once error handling implemented |
||
| </div> | ||
| `, | ||
| ` | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of removing these lines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing. Expression 'foo' is evaluated and since it's invalid test fails once error handling is happening.