properly addes a default onFailure method to all Requests

fixes Bug 733639 - Unable to add Vold Utils and Menuitems libraries into an addon.
This commit is contained in:
Sean McArthur 2012-03-07 17:19:48 -08:00
Родитель d47c9ec61c
Коммит 3ec1e71154
1 изменённых файлов: 19 добавлений и 6 удалений

Просмотреть файл

@ -3,19 +3,22 @@
var Request = require('shipyard/http/Request'),
Cookie = require('shipyard/utils/Cookie'),
log = require('shipyard/utils/log'),
dom = require('shipyard/dom'),
fd = dom.window.get('fd');
dom = require('shipyard/dom');
function fd() {
return dom.window.get('fd');
}
Request.prototype.options.headers['X-CSRFToken'] = Cookie.read('csrftoken');
Request.prototype.options.onFailure = function(text) {
var oldInit = Request.prototype.initialize;
var defaultFailure = function(text) {
if (this.status !== 0 && text) {
var response;
try {
response = JSON.parse(text);
} catch (notJSON) {
log.warn('Response error is not valid JSON', text);
log.warn('Response error is not valid JSON');
if (text.indexOf('<html') !== -1) {
// We somehow got a full HTML page. Bad!
log.error('Response is an HTML page!');
@ -26,6 +29,16 @@ Request.prototype.options.onFailure = function(text) {
}
}
fd.error.alert(this.xhr.statusText, response);
fd().error.alert(this.xhr.statusText, response);
}
};
Request.implement('initialize', function FD_Request(options) {
if (!options) {
options = {};
}
if (!options.onFailure) {
options.onFailure = defaultFailure;
}
oldInit.call(this, options);
});