Commit 727a1ed8 authored by Romain Courteaud's avatar Romain Courteaud 🐙

Add reportGadgetDeclarationError acquired method.

This can be used to catched loading errors from HTML gadget declaration.
This will prevent the parent gadget to fail.
parent 19ec19e9
...@@ -426,7 +426,20 @@ ...@@ -426,7 +426,20 @@
scope, scope,
url, url,
sandbox, sandbox,
i; i,
context = this;
function prepareReportGadgetDeclarationError(scope) {
return function (error) {
var aq_dict = context.__acquired_method_dict || {},
method_name = 'reportGadgetDeclarationError';
if (aq_dict.hasOwnProperty(method_name)) {
return aq_dict[method_name].apply(context,
[arguments, scope]);
}
throw error;
};
}
for (i = 0; i < element_list.length; i += 1) { for (i = 0; i < element_list.length; i += 1) {
element = element_list[i]; element = element_list[i];
...@@ -434,11 +447,14 @@ ...@@ -434,11 +447,14 @@
url = element.getAttribute("data-gadget-url"); url = element.getAttribute("data-gadget-url");
sandbox = element.getAttribute("data-gadget-sandbox"); sandbox = element.getAttribute("data-gadget-sandbox");
if (url !== null) { if (url !== null) {
promise_list.push(this.declareGadget(url, { promise_list.push(
element: element, context.declareGadget(url, {
scope: scope || undefined, element: element,
sandbox: sandbox || undefined scope: scope || undefined,
})); sandbox: sandbox || undefined
})
.push(undefined, prepareReportGadgetDeclarationError(scope))
);
} }
} }
...@@ -664,6 +680,8 @@ ...@@ -664,6 +680,8 @@
}; };
RenderJSGadget.declareAcquiredMethod("aq_reportServiceError", RenderJSGadget.declareAcquiredMethod("aq_reportServiceError",
"reportServiceError"); "reportServiceError");
RenderJSGadget.declareAcquiredMethod("aq_reportGadgetDeclarationError",
"reportGadgetDeclarationError");
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// RenderJSGadget.allowPublicAcquisition // RenderJSGadget.allowPublicAcquisition
......
...@@ -4327,6 +4327,9 @@ ...@@ -4327,6 +4327,9 @@
html_url = 'https://example.org/files/qunittest/test12345.html', html_url = 'https://example.org/files/qunittest/test12345.html',
html_url2 = 'https://example.org/files/qunittest/test12346.html'; html_url2 = 'https://example.org/files/qunittest/test12346.html';
gadget.__sub_gadget_dict = {}; gadget.__sub_gadget_dict = {};
gadget.__aq_parent = function (method_name, argument_list) {
throw new renderJS.AcquisitionError("Can not handle " + method_name);
};
this.server.respondWith("GET", html_url, [200, { this.server.respondWith("GET", html_url, [200, {
"Content-Type": "text/html" "Content-Type": "text/html"
...@@ -4349,6 +4352,108 @@ ...@@ -4349,6 +4352,108 @@
.always(start); .always(start);
}); });
test('can catch if declareGadget with scope in HTML fail', function () {
// Subclass RenderJSGadget to not pollute its namespace
var ParentKlass = function () {
RenderJSGadget.call(this);
},
gadget,
error_context,
// catched_error,
html_url = 'https://example.org/files/qunittest/test12345.html',
html_url2 = 'https://example.org/files/qunittest/test12346.html';
ParentKlass.prototype = new RenderJSGadget();
ParentKlass.prototype.constructor = ParentKlass;
ParentKlass.prototype.__acquired_method_dict = {};
ParentKlass.allowPublicAcquisition = RenderJSGadget.allowPublicAcquisition;
gadget = new ParentKlass();
gadget.__sub_gadget_dict = {};
this.server.respondWith("GET", html_url, [200, {
"Content-Type": "text/html"
}, "<html><body><div data-foo='bar' " +
"data-gadget-scope='foo' data-gadget-url='" + html_url2 +
"'></div></body></html>"]);
this.server.respondWith("GET", html_url2, [403, {
"Content-Type": "text/html"
}, "raw html"]);
stop();
expect(5);
renderJS.declareGadgetKlass(html_url)
.then(function (Klass) {
Klass.allowPublicAcquisition('reportGadgetDeclarationError',
function (argument_list, scope) {
var catched_error = argument_list[0];
error_context = this;
equal(catched_error.status, 403);
equal(catched_error.url, html_url2);
equal(scope, 'foo');
});
return gadget.declareGadget(html_url);
})
.then(function (result) {
deepEqual(result, error_context);
ok(true, 'Error correctly catched');
})
.fail(function (e) {
ok(false, "Error should have been catched");
})
.always(start);
});
test('can catch if declareGadget without scope in HTML fail', function () {
// Subclass RenderJSGadget to not pollute its namespace
var ParentKlass = function () {
RenderJSGadget.call(this);
},
gadget,
error_context,
// catched_error,
html_url = 'https://example.org/files/qunittest/test1234598.html',
html_url2 = 'https://example.org/files/qunittest/test1234698.html';
ParentKlass.prototype = new RenderJSGadget();
ParentKlass.prototype.constructor = ParentKlass;
ParentKlass.prototype.__acquired_method_dict = {};
ParentKlass.allowPublicAcquisition = RenderJSGadget.allowPublicAcquisition;
gadget = new ParentKlass();
gadget.__sub_gadget_dict = {};
this.server.respondWith("GET", html_url, [200, {
"Content-Type": "text/html"
}, "<html><body><div data-foo='bar' " +
"data-gadget-url='" + html_url2 +
"'></div></body></html>"]);
this.server.respondWith("GET", html_url2, [403, {
"Content-Type": "text/html"
}, "raw html"]);
stop();
expect(5);
renderJS.declareGadgetKlass(html_url)
.then(function (Klass) {
Klass.allowPublicAcquisition('reportGadgetDeclarationError',
function (argument_list, scope) {
var catched_error = argument_list[0];
error_context = this;
equal(catched_error.status, 403);
equal(catched_error.url, html_url2);
equal(scope, null);
});
return gadget.declareGadget(html_url);
})
.then(function (result) {
deepEqual(result, error_context);
ok(true, 'Error correctly catched');
})
.fail(function (e) {
ok(false, "Error should have been catched");
})
.always(start);
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// RenderJSGadget.declareGadget (iframe) // RenderJSGadget.declareGadget (iframe)
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment