Bläddra i källkod

improve watcher error messages + use debugger when debug: true

Evan You 11 år sedan
förälder
incheckning
cdd1e095b5
4 ändrade filer med 27 tillägg och 17 borttagningar
  1. 8 2
      src/util/debug.js
  2. 13 5
      src/watcher.js
  3. 0 10
      test/unit/specs/util/debug_spec.js
  4. 6 0
      test/unit/specs/watcher_spec.js

+ 8 - 2
src/util/debug.js

@@ -32,8 +32,14 @@ function enableDebug () {
   exports.warn = function (msg) {
     if (hasConsole && !config.silent) {
       console.warn('[Vue warn]: ' + msg)
-      if (config.debug && console.trace) {
-        console.trace()
+      /* istanbul ignore if */
+      if (config.debug) {
+        /* jshint debug: true */
+        debugger
+      } else {
+        console.log(
+          'Set `Vue.config.debug = true` to enable debug mode.'
+        )
       }
     }
   }

+ 13 - 5
src/watcher.js

@@ -77,7 +77,10 @@ p.get = function () {
   try {
     value = this.getter.call(vm, vm)
   } catch (e) {
-    _.warn(e)
+    _.warn(
+      'Error when evaluating expression "' +
+      this.expression + '":\n   ' + e
+    )
   }
   // "touch" every property so they are all tracked as
   // dependencies for deep watching
@@ -102,7 +105,12 @@ p.set = function (value) {
   )
   try {
     this.setter.call(vm, vm, value)
-  } catch (e) {}
+  } catch (e) {
+    _.warn(
+      'Error when evaluating setter "' +
+      this.expression + '":\n   ' + e
+    )
+  }
 }
 
 /**
@@ -134,10 +142,10 @@ p.afterGet = function () {
  */
 
 p.update = function () {
-  if (config.async) {
-    batcher.push(this)
-  } else {
+  if (!config.async || config.debug) {
     this.run()
+  } else {
+    batcher.push(this)
   }
 }
 

+ 0 - 10
test/unit/specs/util/debug_spec.js

@@ -40,15 +40,5 @@ if (typeof console !== 'undefined') {
       expect(console.warn).not.toHaveBeenCalled()
     })
 
-    if (console.trace) {
-      it('trace when not silent and debugging', function () {
-        config.debug = true
-        config.silent = false
-        _.warn('haha')
-        expect(console.trace).toHaveBeenCalled()
-        config.debug = false
-        config.silent = true
-      })
-    }
   })
 }

+ 6 - 0
test/unit/specs/watcher_spec.js

@@ -391,4 +391,10 @@ describe('Watcher', function () {
     expect(_.warn).toHaveBeenCalled()
   })
 
+  it('warn setter errors', function () {
+    var watcher = new Watcher(vm, 'a + b', spy)
+    watcher.set(123)
+    expect(_.warn).toHaveBeenCalled()
+  })
+
 })