Skip to content

Commit 4dff99d

Browse files
committed
improve warning component tracing (close vuejs#5389)
1 parent 98326ce commit 4dff99d

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/core/util/debug.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ if (process.env.NODE_ENV !== 'production') {
1414

1515
warn = (msg, vm) => {
1616
if (hasConsole && (!config.silent)) {
17-
console.error(`[Vue warn]: ${msg} ` + (
18-
vm ? formatLocation(formatComponentName(vm)) : ''
17+
console.error(`[Vue warn]: ${msg}` + (
18+
vm ? generateComponentTrace(vm) : ''
1919
))
2020
}
2121
}
2222

2323
tip = (msg, vm) => {
2424
if (hasConsole && (!config.silent)) {
25-
console.warn(`[Vue tip]: ${msg} ` + (
26-
vm ? formatLocation(formatComponentName(vm)) : ''
25+
console.warn(`[Vue tip]: ${msg}` + (
26+
vm ? generateComponentTrace(vm) : ''
2727
))
2828
}
2929
}
@@ -52,11 +52,23 @@ if (process.env.NODE_ENV !== 'production') {
5252
)
5353
}
5454

55-
const formatLocation = str => {
56-
if (str === `<Anonymous>`) {
57-
str += ` - use the "name" option for better debugging messages.`
55+
const generateComponentTrace = vm => {
56+
if (vm._isVue && vm.$parent && String.prototype.repeat) {
57+
const tree = []
58+
while (vm) {
59+
tree.push(vm)
60+
vm = vm.$parent
61+
}
62+
return '\n\nfound in\n\n' + tree
63+
.map((vm, i) => `${
64+
i === 0 ? '---> ' : ' '.repeat(5 + i * 2)
65+
}${
66+
formatComponentName(vm)
67+
}`)
68+
.join('\n')
69+
} else {
70+
return `\n\n(found in ${formatComponentName(vm)})`
5871
}
59-
return `\n(found in ${str})`
6072
}
6173
}
6274

src/core/util/error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function handleError (err, vm, info) {
77
config.errorHandler.call(null, err, vm, info)
88
} else {
99
if (process.env.NODE_ENV !== 'production') {
10-
warn(`Error in ${info}:`, vm)
10+
warn(`Error in ${info}: "${err.toString()}"`, vm)
1111
}
1212
/* istanbul ignore else */
1313
if (inBrowser && typeof console !== 'undefined') {

0 commit comments

Comments
 (0)