Skip to content

Commit 9bd89f1

Browse files
committed
For macOS app, add catch for JS error, displayign a dialog to the user.
Its a start of handling this a little better - but hopefully the user never sees this...
1 parent 44d0f49 commit 9bd89f1

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/InterSpecApp.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,27 @@ void InterSpecApp::setupWidgets( const bool attemptStateLoad )
514514
delete m_viewer;
515515
m_viewer = nullptr;
516516
}
517-
517+
518518
if( m_layout )
519519
{
520520
delete m_layout;
521521
root()->clear();
522522
}
523-
524-
523+
524+
525+
#if( BUILD_AS_OSX_APP )
526+
// Inject JavaScript to catch errors, that the objective-c will then recieve.
527+
const string jsErrorCode = "window.onerror = function(message, source, lineno, colno, error) {\n"
528+
" console.error( 'JS Error:', message, ', from source:', source, ', lineno:', lineno, ', error:', error );\n"
529+
" window.webkit.messageHandlers.jsErrorHandler.postMessage({\n"
530+
" message: message, source: source, lineno: lineno, colno: colno, error: error ? error.toString() : null\n"
531+
" });\n"
532+
"};";
533+
534+
doJavaScript( jsErrorCode );
535+
#endif // #if( BUILD_AS_OSX_APP )
536+
537+
525538
try
526539
{
527540
m_viewer = new InterSpec();

target/osx/AppDelegate.mm

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,10 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
429429
// does something like:
430430
// window.webkit.messageHandlers.interOp.postMessage({"action": "DoSomething"});
431431
[webConfig.userContentController addScriptMessageHandler: self name:@"interOp"];
432-
432+
433+
// Add a script message handler
434+
[webConfig.userContentController addScriptMessageHandler:self name:@"jsErrorHandler"];
435+
433436
//Some additional settings we may want to set:
434437
// see more at http://jonathanblog2000.blogspot.com/2016/11/understanding-ios-wkwebview.html
435438
//[prefs setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
@@ -1179,16 +1182,41 @@ - (void)applicationWillTerminate:(NSNotification *)notification
11791182
// This method is for WKScriptMessageHandler, and is triggered each time 'interOp' is sent a
11801183
// message from the JavaScript code with something like:
11811184
// window.webkit.messageHandlers.interOp.postMessage({"val": 1});
1185+
// window.webkit.messageHandlers.jsErrorHandler.postMessage({"message": message, "source": source, ...} );
11821186
- (void)userContentController:(WKUserContentController *)userContentController
11831187
didReceiveScriptMessage:(WKScriptMessage *)message{
11841188
NSDictionary *sentData = (NSDictionary*)message.body;
1185-
1189+
11861190
if( sentData == nil )
11871191
{
11881192
NSLog( @"didReceiveScriptMessage: got nil dictionary" );
11891193
return;
11901194
}
1191-
1195+
1196+
if ([message.name isEqualToString:@"jsErrorHandler"]) {
1197+
NSString *errorMessage = sentData[@"message"];
1198+
NSString *source = sentData[@"source"];
1199+
NSNumber *lineNumber = sentData[@"lineno"];
1200+
NSNumber *columnNumber = sentData[@"colno"];
1201+
NSString *errorDetails = sentData[@"error"];
1202+
1203+
// Create the alert
1204+
NSAlert *alert = [[NSAlert alloc] init];
1205+
alert.messageText = @"JavaScript Error";
1206+
alert.informativeText = [NSString stringWithFormat:@"Unexpected JavaScript Error - please consider reporting to [email protected] and restarting app:\n\nMessage: %@\nSource: %@\nLine: %@\nColumn: %@\nDetails: %@",
1207+
errorMessage ?: @"Unknown",
1208+
source ?: @"Unknown",
1209+
lineNumber ?: @0,
1210+
columnNumber ?: @0,
1211+
errorDetails ?: @"No additional details"];
1212+
alert.alertStyle = NSAlertStyleWarning;
1213+
1214+
[alert addButtonWithTitle:@"OK"];
1215+
[alert runModal];
1216+
1217+
return;
1218+
}//if( a JS error )
1219+
11921220
id val = [sentData objectForKey: @"action"];
11931221
if( val != (id)[NSNull null] )
11941222
{

0 commit comments

Comments
 (0)