Skip to content

Commit b2530e0

Browse files
nikhilgabaai
authored andcommitted
Updated walk() to print out more information on exception
1 parent d752d26 commit b2530e0

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/container.es6

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,18 @@ class Container extends Node {
105105
*/
106106
walk (callback) {
107107
return this.each((child, i) => {
108-
let result = callback(child, i)
108+
let result
109+
try {
110+
result = callback(child, i)
111+
} catch (e) {
112+
e.postcssNode = child
113+
if (e.stack && child.source && /\n\s{4}at /.test(e.stack)) {
114+
const s = child.source
115+
e.stack = e.stack.replace(/\n\s{4}at /,
116+
`$&${ s.input.from }:${ s.start.line }:${ s.start.column }$&`)
117+
}
118+
throw e
119+
}
109120
if (result !== false && child.walk) {
110121
result = child.walk(callback)
111122
}

test/container.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ it('walk() breaks iteration', () => {
182182
expect(indexes).toEqual([0])
183183
})
184184

185+
it('walk() adds CSS position to error stack', () => {
186+
const error = new Error('T')
187+
error.stack = 'Error: T\n at b (b.js:2:4)\n at a (a.js:2:1)'
188+
const root = parse(example, { from: '/c.css' })
189+
let catched
190+
try {
191+
root.walk(() => {
192+
throw error
193+
})
194+
} catch (e) {
195+
catched = e
196+
}
197+
expect(catched).toBe(error)
198+
expect(catched.postcssNode.toString()).toEqual('a { a: 1; b: 2 }')
199+
expect(catched.stack).toEqual(
200+
'Error: T\n at /c.css:1:1\n at b (b.js:2:4)\n at a (a.js:2:1)')
201+
})
202+
185203
it('walkDecls() iterates', () => {
186204
const props = []
187205
const indexes = []

0 commit comments

Comments
 (0)