@@ -99,46 +99,54 @@ module.exports.parseStackBetter = function parseStackBetter(err, cb) {
9999} ;
100100
101101module . exports . parseStack = function parseStack ( stack , cb ) {
102- // grab all lines except the first
103- var lines = stack . split ( '\n' ) . slice ( 1 ) , callbacks = lines . length , frames = [ ] , cache = { } ;
102+ try {
103+ // grab all lines except the first
104+ var lines = stack . split ( '\n' ) . slice ( 1 ) , callbacks = lines . length , frames = [ ] , cache = { } ;
105+
106+ if ( lines . length === 0 ) {
107+ throw new Error ( 'No lines to parse!' ) ;
108+ }
104109
105- lines . forEach ( function ( line , index ) {
106- var data = line . match ( / ^ \s * a t (?: ( .+ (?: \[ \w \s + \] ) ? ) ) ? \( ? ( .+ ?) (?: : ( \d + ) : ( \d + ) ) ? \) ? $ / ) . slice ( 1 ) ,
107- frame = {
108- filename : data [ 1 ] ,
109- lineno : ~ ~ data [ 2 ]
110- } ;
110+ lines . forEach ( function ( line , index ) {
111+ var data = line . match ( / ^ \s * a t (?: ( .+ (?: \[ \w \s + \] ) ? ) ) ? \( ? ( .+ ?) (?: : ( \d + ) : ( \d + ) ) ? \) ? $ / ) . slice ( 1 ) ,
112+ frame = {
113+ filename : data [ 1 ] ,
114+ lineno : ~ ~ data [ 2 ]
115+ } ;
111116
112- // only set the function key if it exists
113- if ( data [ 0 ] ) {
114- frame [ 'function' ] = data [ 0 ] ;
115- }
116- // internal Node files are not full path names. Ignore them.
117- if ( frame . filename [ 0 ] === '/' || frame . filename [ 0 ] === '.' ) {
118- // check if it has been read in first
119- if ( frame . filename in cache ) {
120- parseLines ( cache [ frame . filename ] ) ;
121- if ( -- callbacks === 0 ) cb ( null , frames ) ;
122- } else {
123- fs . readFile ( frame . filename , function ( err , file ) {
124- if ( ! err ) {
125- file = file . toString ( ) . split ( '\n' ) ;
126- cache [ frame . filename ] = file ;
127- parseLines ( file ) ;
128- }
129- frames [ index ] = frame ;
117+ // only set the function key if it exists
118+ if ( data [ 0 ] ) {
119+ frame [ 'function' ] = data [ 0 ] ;
120+ }
121+ // internal Node files are not full path names. Ignore them.
122+ if ( frame . filename [ 0 ] === '/' || frame . filename [ 0 ] === '.' ) {
123+ // check if it has been read in first
124+ if ( frame . filename in cache ) {
125+ parseLines ( cache [ frame . filename ] ) ;
130126 if ( -- callbacks === 0 ) cb ( null , frames ) ;
131- } ) ;
127+ } else {
128+ fs . readFile ( frame . filename , function ( err , file ) {
129+ if ( ! err ) {
130+ file = file . toString ( ) . split ( '\n' ) ;
131+ cache [ frame . filename ] = file ;
132+ parseLines ( file ) ;
133+ }
134+ frames [ index ] = frame ;
135+ if ( -- callbacks === 0 ) cb ( null , frames ) ;
136+ } ) ;
137+ }
138+ } else {
139+ frames [ index ] = frame ;
140+ if ( -- callbacks === 0 ) cb ( null , frames ) ;
132141 }
133- } else {
134- frames [ index ] = frame ;
135- if ( -- callbacks === 0 ) cb ( null , frames ) ;
136- }
137142
138- function parseLines ( lines ) {
139- frame . pre_context = lines . slice ( Math . max ( 0 , frame . lineno - ( LINES_OF_CONTEXT + 1 ) ) , frame . lineno - 1 ) ;
140- frame . context_line = lines [ frame . lineno - 1 ] ;
141- frame . post_context = lines . slice ( frame . lineno , frame . lineno + LINES_OF_CONTEXT ) ;
142- }
143- } ) ;
143+ function parseLines ( lines ) {
144+ frame . pre_context = lines . slice ( Math . max ( 0 , frame . lineno - ( LINES_OF_CONTEXT + 1 ) ) , frame . lineno - 1 ) ;
145+ frame . context_line = lines [ frame . lineno - 1 ] ;
146+ frame . post_context = lines . slice ( frame . lineno , frame . lineno + LINES_OF_CONTEXT ) ;
147+ }
148+ } ) ;
149+ } catch ( e ) {
150+ cb ( new Error ( 'Can\'t parse stack trace:\n' + stack ) ) ;
151+ }
144152} ;
0 commit comments