Skip to content

Commit 63a0174

Browse files
Default to re-throwing errors that occur inside of Ajax callbacks, rather than silently ignoring them. To restore the old behavior, pass in an empty function to onException. (Closes prototypejs#302)
1 parent 5140d0c commit 63a0174

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/prototype/ajax.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@
117117
* `onFailure`. Happens _before_ `onComplete`.
118118
* * `onException`: Triggered whenever an XHR error arises. Has a custom
119119
* signature: the first argument is the requester (i.e. an [[Ajax.Request]]
120-
* instance), and the second is the exception object.
120+
* instance), and the second is the exception object. The default
121+
* `onException` handler simply re-throws the caught exception. (If you
122+
* want exceptions to be silently ignored, pass in an empty function to
123+
* `onException`.)
121124
* * `onComplete`: Triggered at the _very end_ of a request's life-cycle, after
122125
* the request completes, status-specific callbacks are called, and possible
123126
* automatic behaviors are processed. Guaranteed to run regardless of what

src/prototype/ajax/base.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ Ajax.Base = Class.create({
88
encoding: 'UTF-8',
99
parameters: '',
1010
evalJSON: true,
11-
evalJS: true
11+
evalJS: true,
12+
onException: function (request, error) {
13+
throw error;
14+
}
1215
};
1316
Object.extend(this.options, options || { });
1417

test/unit/tests/ajax.test.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ function assertContent(id, content, message) {
5757
var extendDefault = function(options) {
5858
return Object.extend({
5959
asynchronous: false,
60-
method: 'get',
61-
onException: function(r, e) { throw e; }
60+
method: 'get'
6261
}, options);
6362
};
6463

@@ -110,15 +109,18 @@ suite("Ajax", function () {
110109
method: 'GET',
111110
onComplete: function () {
112111
assert.equal(1, Ajax.activeRequestCount);
112+
setTimeout(function () {
113+
assert.equal(0, Ajax.activeRequestCount);
114+
done();
115+
}, 250);
113116
throw new Error('test');
117+
},
118+
119+
onException: function () {
120+
// Empty function to prevent the error from being rethrown
114121
}
115122
});
116123

117-
setTimeout(function () {
118-
assert.equal(0, Ajax.activeRequestCount);
119-
done();
120-
}, 1000);
121-
122124
});
123125

124126
suite('Updater', function () {
@@ -540,5 +542,18 @@ suite("Ajax", function () {
540542
}));
541543
});
542544

545+
test('no exception handler', function (done) {
546+
new Ajax.Request('/inspect', extendDefault({
547+
onSuccess: function () {
548+
try {
549+
throw new Error("foo");
550+
assert(true);
551+
} finally {
552+
done();
553+
}
554+
}
555+
}));
556+
});
557+
543558
});
544559

0 commit comments

Comments
 (0)