Merge pull request +11473 from c9/require-cache-error

improve error handlidng of cached require
smf-sdk
Lennart Kats 2016-01-08 13:19:07 +01:00
commit 0b52a17e78
1 zmienionych plików z 20 dodań i 5 usunięć

Wyświetl plik

@ -443,9 +443,15 @@ var loadCached = function(path, cb) {
return loadText(path, cb); return loadText(path, cb);
function loadNew() { function loadNew() {
loadText(path, function(e, val, xhr) { loadText(path, function(e, val, xhr) {
try {
var m = cb(e, val); var m = cb(e, val);
if (val) { } catch(err) {
ideCache.delete(path);
e = err;
}
if (!e) {
var ETAG = xhr.getResponseHeader("ETAG"); var ETAG = xhr.getResponseHeader("ETAG");
if (!ETAG) return;
var res = new Response(val); var res = new Response(val);
res.headers.set("ETAG", ETAG); res.headers.set("ETAG", ETAG);
var req = new Request(path); var req = new Request(path);
@ -467,13 +473,16 @@ var loadCached = function(path, cb) {
ideCache.match(path).then(function(e) { ideCache.match(path).then(function(e) {
if (!e) if (!e)
return loadNew(); return loadNew();
e.text().then(function(val) { return e.text().then(function(val) {
var deps = e.headers.get("deps"); var deps = e.headers.get("deps");
if (typeof deps == "string") if (typeof deps == "string")
deps = deps ? deps.split(",") : []; deps = deps ? deps.split(",") : [];
cb(null, val, deps); cb(null, val, deps);
}); });
}).catch(function() {
loadNew();
ideCache.delete(path);
}); });
}; };
@ -532,9 +541,15 @@ function post(path, val, progress, cb) {
var xhr = new window.XMLHttpRequest(); var xhr = new window.XMLHttpRequest();
xhr.open("POST", path, true); xhr.open("POST", path, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.onload = function(e) { cb(null, xhr.responseText, xhr); }; xhr.onload = function(e) {
if (xhr.status > 399 && xhr.status < 600)
return cb(xhr);
cb(null, xhr.responseText, xhr);
};
xhr.onreadystatechange = function(e) { progress(xhr.responseText, xhr); }; xhr.onreadystatechange = function(e) { progress(xhr.responseText, xhr); };
xhr.onabort = xhr.onerror = function(e) { cb(e); }; xhr.onabort = xhr.onerror = function(e) {
xhr && cb(e);
};
xhr.send(val); xhr.send(val);
} }