Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added message "stack trace" in case of an uncaught Error

  • Loading branch information...
commit e976dd127e66f225775d177a524998600d914e06 1 parent 74083fa
@tristanls authored
Showing with 70 additions and 6 deletions.
  1. +1 −1  README.md
  2. +69 −5 lib/anode/configuration.js
View
2  README.md
@@ -149,7 +149,7 @@ Other examples show simpler functionality:
2. `valuematch.example.coffee` shows how `$` syntax matches pattern by evaluating the variable, run `coffee valuematch.example.coffee`
3. `simple.example.coffee` shows a simple counter system, run `coffee simple.example.coffee`
4. `optional.example.coffee` shows use of optional parameters in a message, run `coffee optional.example.coffee`
-5. `assert.example.coffee` shows the user of assertions, run `coffee assert.example.coffee`
+5. `assert.example.coffee` shows the use of assertions as well as demonstrating the `anode` message "stack trace" in case an unexpected error occurs, run `coffee assert.example.coffee`
## License
View
74 lib/anode/configuration.js
@@ -34,7 +34,8 @@ var Configuration = exports.Configuration = function ( options ) {
var __actorBehaviorStore = {}, // used for storing actor behaviors
__actorReferenceStore = {}, // used for actor address security
__configurationUniqueId = 1, // start with one for easier boolean checks
- __bus = new AsyncEventEmitter();
+ __bus = new AsyncEventEmitter(),
+ __stackTrace = []; // 10 deep message stack trace for report errors
//
// ### function createActor
@@ -108,6 +109,7 @@ var Configuration = exports.Configuration = function ( options ) {
// copy arguments for this message
var _args = Array.prototype.slice.call( args, 0 );
+ _addToStackTrace( __stackTrace, { from: fromActor, to: actor, msg: _args } );
if ( self.__debug ) _logMessage( fromActor, actor, _args );
// add address of the actor
@@ -120,6 +122,7 @@ var Configuration = exports.Configuration = function ( options ) {
} // if actors is Array
else {
+ _addToStackTrace( __stackTrace, { from: fromActor, to: actors, msg: args } );
if ( self.__debug ) _logMessage( fromActor, actors, args );
// add address of the actor
@@ -145,7 +148,32 @@ var Configuration = exports.Configuration = function ( options ) {
if ( ! _validateActorBehavior( actor, behavior, __actorBehaviorStore ) )
throw new Error();
- behavior.apply( actor, args );
+ try {
+ behavior.apply( actor, args );
+ } catch ( error ) {
+
+ // message stack trace
+ console.error( '\nError occured\n' +
+ 'Message history trace (*message order is ' +
+ 'non-deterministic):\n' );
+
+ for ( var i = 0; i < __stackTrace.length; i++ ) {
+
+ var msg = __stackTrace[ i ];
+
+ if ( msg.msg )
+ msg.msg.shift(); // remove the __uid::message text
+
+ _logMessage( msg.from, msg.to, msg.msg, 'error' );
+
+ } // for i in __stackTrace.length
+
+ if ( __stackTrace.length >= 10 )
+ console.error( '...' );
+
+ throw error;
+
+ } // catch error
}; // __handle
@@ -270,7 +298,22 @@ var _actorToString = function ( actor ) {
}; // _actorToString
-var _logMessage = function ( from, to, message ) {
+//
+// ### function _addToStackTrace ( stackTrace, msg )
+// #### @stackTrace {array} message stack trace maintained by the configuration
+// #### @msg {object} message object containing from, to, and message info
+// Adds the message to the stack trace stack, maintaining stack size at 10
+//
+var _addToStackTrace = function ( stackTrace, msg ) {
+
+ while ( stackTrace.length >= 10 )
+ stackTrace.pop();
+
+ Array.prototype.unshift.call( stackTrace, msg );
+
+}; // _addToStackTrace
+
+var _logMessage = function ( from, to, message, level ) {
var __from = 'FROM ' + _actorToString( from );
@@ -309,7 +352,7 @@ var _logMessage = function ( from, to, message ) {
partialMsg = portion.toString();
- }
+ } // else
} // else
@@ -317,7 +360,28 @@ var _logMessage = function ( from, to, message ) {
} // for i in message.length
- console.log( __from, __to, __send, msg );
+ if ( level ) {
+
+ switch ( level ) {
+
+ case 'error' :
+
+ console.error( __from, __to, __send, msg );
+ break; // case error
+
+ default :
+
+ console.log( __from, __to, __send, msg );
+ break; // default
+
+ } // switch level
+
+ } // if level
+ else {
+
+ console.log( __from, __to, __send, msg );
+
+ } // else
}; // _logMessage
Please sign in to comment.
Something went wrong with that request. Please try again.