Skip to content

Commit 346da4f

Browse files
committed
Try t ohandle JS exceptions a little more gracefully.
This is an experiment.
1 parent 34c0c31 commit 346da4f

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

InterSpec/InterSpecApp.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ class InterSpec_API InterSpecApp : public Wt::WApplication
8080
//isAndroid(): Searches for Android in the user agent string
8181
bool isAndroid() const;
8282

83-
#if( WT_VERSION>=0x3030400 )
84-
//ToDo: define a custom handler for javascript errors
85-
// virtual void handleJavaScriptError(const std::string& errorText)
86-
// {
87-
// std::cerr << "JS error: " << errorText << std::endl;
88-
// }
89-
#endif
9083

9184
/* svlog send the message to the InterSpec for display to the user. */
9285
void svlog( const Wt::WString& message, int priority = 1 );
@@ -266,8 +259,10 @@ class InterSpec_API InterSpecApp : public Wt::WApplication
266259
/** Adds `m_activeTimeSinceDbUpdate` to the database, saving the updated result. */
267260
void updateUsageTimeToDb();
268261

262+
#if( WT_VERSION>=0x3030400 )
269263
virtual void handleJavaScriptError( const std::string &errorText );
270-
264+
#endif
265+
271266
#if( !BUILD_FOR_WEB_DEPLOYMENT )
272267
/** Checks for URL argument "externalid", or equivalently "apptoken", and if found sets m_externalToken to it.
273268

InterSpec/InterSpecServer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ namespace InterSpecServer
190190
/** Sets the session as dead. */
191191
InterSpec_API void set_session_destructing( const char *session_token );
192192

193+
/** Sets marks the session as being allowed to be loaded again.
194+
195+
This is used when session JS crashes.
196+
197+
If session token does not already exist, it will not be added.
198+
199+
Returns true if successful (found session token, and reset state).
200+
*/
201+
InterSpec_API bool set_session_reload_allow( const char *session_token );
202+
193203
#if( !BUILD_FOR_WEB_DEPLOYMENT )
194204
/** Open one or more files from the filesystem. For macOS this would be
195205
dropping a file to the app icon in the Finder. Or on Windows it would be

src/InterSpecApp.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ bool InterSpecApp::checkExternalTokenFromUrl()
183183
{
184184
m_externalToken = p.second.front();
185185

186+
InterSpecServer::add_allowed_session_token( m_externalToken.c_str(), InterSpecServer::SessionType::PrimaryAppInstance );
187+
186188
const int status = InterSpecServer::set_session_loaded( m_externalToken.c_str() );
187189

188190
pair<bool,InterSpecServer::SessionType> session_type
@@ -1304,6 +1306,8 @@ void InterSpecApp::prepareForEndOfSession()
13041306
Wt::log("debug") << "Have prepared for end of session " << sessionId() << ".";
13051307
}//void InterSpecApp::prepareForEndOfSession()
13061308

1309+
1310+
#if( WT_VERSION>=0x3030400 )
13071311
void InterSpecApp::handleJavaScriptError( const std::string &errorText )
13081312
{
13091313
const string js_err_msg = WString("There was a javascript error: {1}")
@@ -1331,8 +1335,14 @@ void InterSpecApp::handleJavaScriptError( const std::string &errorText )
13311335

13321336
// Default WApplication implementation logs error, and then calls WApplication:quit()
13331337
WApplication::handleJavaScriptError( errorText );
1338+
1339+
#if( !BUILD_FOR_WEB_DEPLOYMENT )
1340+
// Allow re-loading of the window
1341+
if( !m_externalToken.empty() )
1342+
InterSpecServer::set_session_reload_allow( m_externalToken.c_str() );
1343+
#endif //#if( !BUILD_FOR_WEB_DEPLOYMENT )
13341344
}//void handleJavaScriptError( const std::string &errorText )
1335-
1345+
#endif //#if( WT_VERSION>=0x3030400 )
13361346

13371347
void InterSpecApp::clearSession()
13381348
{

src/InterSpecServer.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,22 @@ void killServer()
762762
}
763763
}//void set_session_destructing( const char *session_token )
764764

765+
766+
bool set_session_reload_allow( const char *session_token )
767+
{
768+
lock_guard<mutex> lock( ns_sessions_mutex );
769+
auto pos = ns_sessions.find( session_token );
770+
if( pos == end(ns_sessions) )
771+
return false;
772+
773+
SessionState &session = pos->second;
774+
session.auth_time = std::chrono::system_clock::now();
775+
session.current_state = SessionState::State::AuthorizedNotLoaded;
776+
777+
return true;
778+
}//bool set_session_reload_allow( const char *session_token )
765779

780+
766781
int session_status( const char *session_token )
767782
{
768783
lock_guard<mutex> lock( ns_sessions_mutex );
@@ -934,7 +949,7 @@ std::string file_to_open_on_load( const std::string &session_token )
934949
auto pos = ns_sessions.find( session_token );
935950
if( pos == end(ns_sessions) )
936951
{
937-
assert( 0 );
952+
//assert( 0 );
938953
return "";
939954
}
940955

0 commit comments

Comments
 (0)