Skip to content

Commit 172f390

Browse files
committed
Replace console.error with reportError (further fixes MithrilJS#2621)
With this change, errors that occur within render() or onmatch() will be reported to the global event handler without interrupting processing. As a result, errors will be forwarded to window.onerror.
1 parent ee42d44 commit 172f390

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

api/mount-redraw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function(render, schedule) {
1010
function sync() {
1111
for (offset = 0; offset < subscriptions.length; offset += 2) {
1212
try { render(subscriptions[offset], Vnode(subscriptions[offset + 1]), redraw) }
13-
catch (e) { console.error(e) }
13+
catch (e) { reportError(e) }
1414
}
1515
offset = -1
1616
}

api/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = function($window, mountRedraw) {
7474
Object.assign(data.params, $window.history.state)
7575

7676
function reject(e) {
77-
console.error(e)
77+
reportError(e)
7878
route.set(fallbackRoute, null, {replace: true})
7979
}
8080

api/tests/test-mountRedraw.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ var coreRenderer = require("../../render/render")
1010
var h = require("../../render/hyperscript")
1111

1212
o.spec("mount/redraw", function() {
13-
var root, m, throttleMock, consoleMock, $document, errors
14-
var realError = console.error
13+
var root, m, throttleMock, $document, errors
14+
var realError = global.reportError
1515
o.beforeEach(function() {
1616
var $window = domMock()
17-
console.error = consoleMock = o.spy()
17+
global.reportError = o.spy()
1818
throttleMock = throttleMocker()
1919
root = $window.document.body
2020
m = mountRedraw(coreRenderer($window), throttleMock.schedule)
@@ -23,11 +23,11 @@ o.spec("mount/redraw", function() {
2323
})
2424

2525
o.afterEach(function() {
26-
o(consoleMock.calls.map(function(c) {
26+
o(global.reportError.calls.map(function(c) {
2727
return c.args[0]
2828
})).deepEquals(errors)
2929
o(throttleMock.queueLength()).equals(0)
30-
console.error = realError
30+
global.reportError = realError
3131
})
3232

3333
o("shouldn't error if there are no renderers", function() {

api/tests/test-router.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ o.spec("route", function() {
6969

7070
// In case it doesn't get reset
7171
var realError = console.error
72+
var realReportError = global.reportError
7273

7374
o.beforeEach(function() {
7475
currentTest = nextID++
@@ -92,6 +93,7 @@ o.spec("route", function() {
9293
o(throttleMock.queueLength()).equals(0)
9394
currentTest = -1 // doesn't match any test
9495
console.error = realError
96+
global.reportError = realReportError
9597
})
9698

9799
o("throws on invalid `root` DOM node", function() {
@@ -1175,7 +1177,7 @@ o.spec("route", function() {
11751177
var renderCount = 0
11761178
var spy = o.spy()
11771179
var error = new Error("error")
1178-
var errorSpy = console.error = o.spy()
1180+
var errorSpy = global.reportError = o.spy()
11791181

11801182
var resolver = {
11811183
onmatch: lock(function() {
@@ -2089,11 +2091,11 @@ o.spec("route", function() {
20892091
}
20902092

20912093
// Errors thrown during redrawing of mounted components are caught in m.mount()
2092-
// and console.error is called.
2093-
// Therefore, spy is used to confirm that console.error is not called
2094+
// and reportError is called.
2095+
// Therefore, spy is used to confirm that reportError is not called
20942096
// when it is first mounted.
2095-
var spy = o.spy(console.error)
2096-
console.error = spy
2097+
var spy = o.spy()
2098+
global.reportError = spy
20972099

20982100
$window.location.href = prefix + "/"
20992101
o(function(){

0 commit comments

Comments
 (0)