Commit dacfeb78 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_jio: wrapper DOMParser().parseFromString for better error handling

   As exposed by Romain, `parseFromString` does not throw any error if the XML parsing fails. Instead, you have to manually check if there is a `parsererror` element inside the returned document.

   See: https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#error_handling
parent cf730f8b
...@@ -9,38 +9,66 @@ ...@@ -9,38 +9,66 @@
var DISPLAY_JSON_FORM = 'display_json_form', var DISPLAY_JSON_FORM = 'display_json_form',
DISPLAY_RAW_XML = 'display_raw_xml'; DISPLAY_RAW_XML = 'display_raw_xml';
//////////////////////////////////////////
// ParserError
//////////////////////////////////////////
function DOMParserError(message) {
this.name = "DOMParserError";
if ((message !== undefined) && (typeof message !== "string")) {
throw new TypeError('You must pass a string for DOMParserError.');
}
this.message = message || "Default Message";
}
DOMParserError.prototype = new Error();
DOMParserError.prototype.constructor = DOMParserError;
//////////////////////////////////////////
// DOMParser
//////////////////////////////////////////
function parseDocumentStringOrFail(string, mime_type) {
var doc = new DOMParser().parseFromString(string, mime_type),
error_node = doc.querySelector('parsererror');
if (error_node !== null) {
// parsing failed
throw new DOMParserError(error_node.textContent);
}
return doc;
}
function jsonDictToParameterXML(json) { function jsonDictToParameterXML(json) {
var parameter_id, var parameter_id,
xml_output = $((new DOMParser()).parseFromString( xml_output = parseDocumentStringOrFail(
'<?xml version="1.0" encoding="UTF-8" ?><instance />', 'text/xml')); '<?xml version="1.0" encoding="UTF-8" ?><instance />',
'text/xml'
),
xml_instance = xml_output.querySelector('instance'),
xml_parameter;
// Used by serialisation XML // Used by serialisation XML
for (parameter_id in json) { for (parameter_id in json) {
if (json.hasOwnProperty(parameter_id)) { if (json.hasOwnProperty(parameter_id)) {
$('instance', xml_output).append( xml_parameter = xml_output.createElement('parameter');
$('<parameter />', xml_output) xml_parameter.textContent = json[parameter_id];
.text(json[parameter_id]) xml_parameter.id = parameter_id;
.attr({id: parameter_id}) xml_instance.appendChild(xml_parameter);
);
} }
} }
return vkbeautify.xml( return vkbeautify.xml(
(new XMLSerializer()).serializeToString(xml_output.context) (new XMLSerializer()).serializeToString(xml_output)
); );
} }
function jsonDictToParameterJSONInXML(json) { function jsonDictToParameterJSONInXML(json) {
var xml_output = $((new DOMParser()).parseFromString( var content = vkbeautify.json(JSON.stringify(json)),
'<?xml version="1.0" encoding="UTF-8" ?><instance />', xml_output = parseDocumentStringOrFail(
'text/xml' '<?xml version="1.0" encoding="UTF-8" ?>' +
)); '<instance><parameter id="_">{}</parameter></instance>',
// Used by serialisation XML 'text/xml'
$('instance', xml_output).append( );
$('<parameter />', xml_output)
.text(vkbeautify.json(JSON.stringify(json))) xml_output.querySelector('parameter[id="_"]').textContent = content;
.attr({id: "_"})
);
return vkbeautify.xml( return vkbeautify.xml(
(new XMLSerializer()).serializeToString(xml_output.context) (new XMLSerializer()).serializeToString(xml_output)
); );
} }
...@@ -877,7 +905,7 @@ ...@@ -877,7 +905,7 @@
if (parameter_xml !== undefined) { if (parameter_xml !== undefined) {
if (serialisation === "json-in-xml") { if (serialisation === "json-in-xml") {
parameter_list = (new DOMParser()).parseFromString( parameter_list = parseDocumentStringOrFail(
parameter_xml, parameter_xml,
'text/xml' 'text/xml'
).querySelectorAll("parameter"); ).querySelectorAll("parameter");
...@@ -885,7 +913,7 @@ ...@@ -885,7 +913,7 @@
if (parameter_list.length > 1) { if (parameter_list.length > 1) {
throw new Error("The current parameter should contains only _ parameter (json-in-xml)."); throw new Error("The current parameter should contains only _ parameter (json-in-xml).");
} }
parameter_entry = (new DOMParser()).parseFromString( parameter_entry = parseDocumentStringOrFail(
parameter_xml, parameter_xml,
'text/xml' 'text/xml'
).querySelector("parameter[id='_']"); ).querySelector("parameter[id='_']");
...@@ -898,7 +926,7 @@ ...@@ -898,7 +926,7 @@
); );
} }
} else if (["", "xml"].indexOf(serialisation) >= 0) { } else if (["", "xml"].indexOf(serialisation) >= 0) {
parameter_entry = (new DOMParser()).parseFromString( parameter_entry = parseDocumentStringOrFail(
parameter_xml, parameter_xml,
'text/xml' 'text/xml'
).querySelector("parameter[id='_']"); ).querySelector("parameter[id='_']");
...@@ -906,7 +934,7 @@ ...@@ -906,7 +934,7 @@
if (parameter_entry !== null) { if (parameter_entry !== null) {
throw new Error("The current parameter values should NOT contains _ parameter (xml)."); throw new Error("The current parameter values should NOT contains _ parameter (xml).");
} }
$((new DOMParser()).parseFromString( $(parseDocumentStringOrFail(
parameter_xml, parameter_xml,
'text/xml' 'text/xml'
).querySelectorAll("parameter")) ).querySelectorAll("parameter"))
......
...@@ -280,7 +280,7 @@ ...@@ -280,7 +280,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1003.60830.54174.62190</string> </value> <value> <string>1003.61829.49064.29661</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -298,7 +298,7 @@ ...@@ -298,7 +298,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1666905733.95</float> <float>1666965524.99</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
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