File tree Expand file tree Collapse file tree 3 files changed +24
-8
lines changed Expand file tree Collapse file tree 3 files changed +24
-8
lines changed Original file line number Diff line number Diff line change 1
1
unreleased
2
2
==========
3
3
4
+ * Fix stack trace for strict json parse error
4
5
* deps: depd@~ 1.1.2
5
6
- perf: remove argument reassignment
6
7
* deps: http-errors@~ 1.6.3
Original file line number Diff line number Diff line change @@ -88,9 +88,7 @@ function json (options) {
88
88
debug ( 'parse json' )
89
89
return JSON . parse ( body , reviver )
90
90
} catch ( e ) {
91
- throw normalizeJsonSyntaxError ( e , {
92
- stack : e . stack
93
- } )
91
+ throw normalizeJsonSyntaxError ( e )
94
92
}
95
93
}
96
94
@@ -208,11 +206,10 @@ function normalizeJsonSyntaxError (error, obj) {
208
206
}
209
207
}
210
208
211
- var props = Object . keys ( obj )
212
-
213
- for ( var j = 0 ; j < props . length ; j ++ ) {
214
- var prop = props [ j ]
215
- error [ prop ] = obj [ prop ]
209
+ if ( obj ) {
210
+ // replace stack before message for Node.js 0.10 and below
211
+ error . stack = obj . stack . replace ( error . message , obj . message )
212
+ error . message = obj . message
216
213
}
217
214
218
215
return error
Original file line number Diff line number Diff line change @@ -273,6 +273,17 @@ describe('bodyParser.json()', function () {
273
273
. send ( 'true' )
274
274
. expect ( 400 , 'entity.parse.failed' , done )
275
275
} )
276
+
277
+ it ( 'should include correct message in stack trace' , function ( done ) {
278
+ request ( this . server )
279
+ . post ( '/' )
280
+ . set ( 'Content-Type' , 'application/json' )
281
+ . set ( 'X-Error-Property' , 'stack' )
282
+ . send ( 'true' )
283
+ . expect ( 400 )
284
+ . expect ( shouldContainInBody ( parseError ( '#rue' ) . replace ( '#' , 't' ) ) )
285
+ . end ( done )
286
+ } )
276
287
} )
277
288
} )
278
289
@@ -639,3 +650,10 @@ function parseError (str) {
639
650
return e . message
640
651
}
641
652
}
653
+
654
+ function shouldContainInBody ( str ) {
655
+ return function ( res ) {
656
+ assert . ok ( res . text . indexOf ( str ) !== - 1 ,
657
+ 'expected \'' + res . text + '\' to contain \'' + str + '\'' )
658
+ }
659
+ }
You can’t perform that action at this time.
0 commit comments