Commit c171e108 authored by Gabriel Monnerat's avatar Gabriel Monnerat

erp5_web_renderjs_ui: Move functions that build error_text from error to global gadget

parent 817bf6bf
/*global window, RSVP, SimpleQuery, ComplexQuery, Query, /*global window, RSVP, SimpleQuery, ComplexQuery, Query,
ensureArray, Array*/ ensureArray, Array, domsugar, Event, XMLHttpRequest, navigator*/
/*jslint indent: 2, maxerr: 3, nomen: true, unparam: true, continue: true */ /*jslint indent: 2, maxerr: 3, nomen: true, unparam: true, continue: true */
(function (window, RSVP, SimpleQuery, ComplexQuery, Query, (function (window, RSVP, SimpleQuery, ComplexQuery, Query,
ensureArray, Array) { ensureArray, Array, domsugar, Event, XMLHttpRequest, navigator) {
"use strict"; "use strict";
/////////////////////////////// ///////////////////////////////
...@@ -292,6 +292,122 @@ ...@@ -292,6 +292,122 @@
return url_mapping; return url_mapping;
} }
function convertOriginalErrorToErrorDataList(original_error) {
return new RSVP.Queue()
.push(function () {
var error_list = [original_error],
i,
error,
error_response,
error_text = "";
// Do not break the application in case of errors.
// Display it to the user for now,
// and allow user to go back to the frontpage
// Add error handling stack
error_list.push(new Error('stopping ERP5JS'));
for (i = 0; i < error_list.length; i += 1) {
error = error_list[i];
if (error instanceof Event) {
error = {
string: error.toString(),
message: error.message,
type: error.type,
target: error.target
};
if (error.target !== undefined) {
error_list.splice(i + 1, 0, error.target);
}
}
if (error instanceof XMLHttpRequest) {
if ((error.getResponseHeader('Content-Type') || "")
.indexOf('text/') === 0) {
error_response = error.response;
}
error = {
message: error.toString(),
readyState: error.readyState,
status: error.status,
statusText: error.statusText,
response: error.response,
responseUrl: error.responseUrl,
response_headers: (error.getAllResponseHeaders() ?
error.getAllResponseHeaders().split('\r\n')
: null)
};
}
if (error.constructor === Array ||
error.constructor === String ||
error.constructor === Object) {
try {
error = JSON.stringify(error, null, ' ');
} catch (ignore) {
}
}
error_text += error.message || error;
error_text += '\n';
if (error.fileName !== undefined) {
error_text += 'File: ' +
error.fileName +
': ' + error.lineNumber + '\n';
}
if (error.stack !== undefined) {
error_text += 'Stack: ' + error.stack + '\n';
}
error_text += '---\n';
}
if (error_response && !error.response) {
error.response = error_response;
}
return [error, error_text];
});
}
function buildErrorElementFromErrorText(error_text) {
return new RSVP.Queue()
.push(function () {
return domsugar("section", [
domsugar("p", {
"text": 'Please report this unhandled error to the support ' +
'team, and go back to the '
}, [
domsugar("a", {
"href": "#",
"text": "homepage"
})
]),
domsugar("br"),
domsugar("p", {
"text": 'Location: '
}, [
domsugar("a", {
"href": window.location.toString(),
"text": window.location.toString()
})
]),
domsugar("p", {
"text": 'User-agent: ' + navigator.userAgent
}),
domsugar("p", {
"text": 'Date: ' + new Date(Date.now()).toISOString()
}),
domsugar("p", {
"text": 'Online: ' + navigator.onLine
}),
domsugar("br"),
domsugar("pre", [
domsugar("code", {
"text": error_text
})
])
]);
});
}
function getListboxClipboardActionList() { function getListboxClipboardActionList() {
var action_list = ensureArray(this.state.erp5_document._links.action_object_list_action || []), var action_list = ensureArray(this.state.erp5_document._links.action_object_list_action || []),
i, i,
...@@ -441,7 +557,10 @@ ...@@ -441,7 +557,10 @@
} }
window.mergeGlobalActionWithRawActionList = mergeGlobalActionWithRawActionList; window.mergeGlobalActionWithRawActionList = mergeGlobalActionWithRawActionList;
window.triggerListboxClipboardAction = triggerListboxClipboardAction; window.triggerListboxClipboardAction = triggerListboxClipboardAction;
window.convertOriginalErrorToErrorDataList = convertOriginalErrorToErrorDataList;
window.buildErrorElementFromErrorText = buildErrorElementFromErrorText;
window.declareGadgetClassCanHandleListboxClipboardAction = window.declareGadgetClassCanHandleListboxClipboardAction =
declareGadgetClassCanHandleListboxClipboardAction; declareGadgetClassCanHandleListboxClipboardAction;
}(window, RSVP, SimpleQuery, ComplexQuery, Query, ensureArray, Array)); }(window, RSVP, SimpleQuery, ComplexQuery, Query, ensureArray, Array, domsugar,
\ No newline at end of file Event, XMLHttpRequest, navigator));
...@@ -236,7 +236,7 @@ ...@@ -236,7 +236,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>991.63236.62412.49203</string> </value> <value> <string>1007.4418.49274.62395</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -256,7 +256,7 @@ ...@@ -256,7 +256,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1620739824.54</float> <float>1678908170.99</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
/*globals window, document, RSVP, rJS, /*globals window, document, RSVP, rJS,
URI, location, XMLHttpRequest, console, navigator, Event, URI, location, XMLHttpRequest, console, navigator, Event,
URL, domsugar*/ URL, domsugar, convertOriginalErrorToErrorDataList,
buildErrorElementFromErrorText*/
/*jslint indent: 2, maxlen: 80, unparam: true*/ /*jslint indent: 2, maxlen: 80, unparam: true*/
(function (window, document, RSVP, rJS, (function (window, document, RSVP, rJS,
XMLHttpRequest, location, console, navigator, Event, XMLHttpRequest, location, console, navigator, Event,
URL, domsugar) { URL, domsugar, convertOriginalErrorToErrorDataList,
buildErrorElementFromErrorText) {
"use strict"; "use strict";
var MAIN_SCOPE = "m", var MAIN_SCOPE = "m",
...@@ -130,71 +132,10 @@ ...@@ -130,71 +132,10 @@
} }
function displayErrorContent(gadget, original_error) { function displayErrorContent(gadget, original_error) {
var error_list = [original_error], return convertOriginalErrorToErrorDataList(original_error)
i, .push(function (error_list) {
error, var error = error_list[0],
error_response, error_text = error_list[1];
error_text = "";
// Do not break the application in case of errors.
// Display it to the user for now,
// and allow user to go back to the frontpage
// Add error handling stack
error_list.push(new Error('stopping ERP5JS'));
for (i = 0; i < error_list.length; i += 1) {
error = error_list[i];
if (error instanceof Event) {
error = {
string: error.toString(),
message: error.message,
type: error.type,
target: error.target
};
if (error.target !== undefined) {
error_list.splice(i + 1, 0, error.target);
}
}
if (error instanceof XMLHttpRequest) {
if ((error.getResponseHeader('Content-Type') || "")
.indexOf('text/') === 0) {
error_response = error.response;
}
error = {
message: error.toString(),
readyState: error.readyState,
status: error.status,
statusText: error.statusText,
response: error.response,
responseUrl: error.responseUrl,
response_headers: (error.getAllResponseHeaders()
? error.getAllResponseHeaders().split('\r\n')
: null)
};
}
if (error.constructor === Array ||
error.constructor === String ||
error.constructor === Object) {
try {
error = JSON.stringify(error, null, ' ');
} catch (ignore) {
}
}
error_text += error.message || error;
error_text += '\n';
if (error.fileName !== undefined) {
error_text += 'File: ' +
error.fileName +
': ' + error.lineNumber + '\n';
}
if (error.stack !== undefined) {
error_text += 'Stack: ' + error.stack + '\n';
}
error_text += '---\n';
}
console.error(original_error); console.error(original_error);
if (original_error instanceof Error) { if (original_error instanceof Error) {
...@@ -204,8 +145,8 @@ ...@@ -204,8 +145,8 @@
// Gadget has not yet been correctly initialized // Gadget has not yet been correctly initialized
throw error; throw error;
} }
if (error_response && error_response.text) { if (error.response && error.response.text) {
return error_response.text().then( return error.response.text().then(
function (request_error_text) { function (request_error_text) {
return gadget.changeState({ return gadget.changeState({
error_text: error_text, error_text: error_text,
...@@ -217,10 +158,10 @@ ...@@ -217,10 +158,10 @@
} }
return gadget.changeState({ return gadget.changeState({
error_text: error_text, error_text: error_text,
request_error_text: error_response, request_error_text: error.response,
url: undefined url: undefined
}); });
});
} }
function displayError(gadget, error) { function displayError(gadget, error) {
...@@ -788,44 +729,13 @@ ...@@ -788,44 +729,13 @@
return; return;
}) })
.push(function () { .push(function () {
var element = gadget.props.content_element, return buildErrorElementFromErrorText(
container; gadget.state.error_text
);
container = domsugar("section", [
domsugar("p", {
"text": 'Please report this unhandled error to the support ' +
'team, and go back to the '
}, [
domsugar("a", {
"href": "#",
"text": "homepage"
})
]),
domsugar("br"),
domsugar("p", {
"text": 'Location: '
}, [
domsugar("a", {
"href": window.location.toString(),
"text": window.location.toString()
})
]),
domsugar("p", {
"text": 'User-agent: ' + navigator.userAgent
}),
domsugar("p", {
"text": 'Date: ' + new Date(Date.now()).toISOString()
}),
domsugar("p", {
"text": 'Online: ' + navigator.onLine
}),
domsugar("br"),
domsugar("pre", [
domsugar("code", {
"text": gadget.state.error_text
}) })
])
]); .push(function (container) {
var element = gadget.props.content_element;
// Remove the content // Remove the content
while (element.firstChild) { while (element.firstChild) {
...@@ -1068,4 +978,6 @@ ...@@ -1068,4 +978,6 @@
}(window, document, RSVP, rJS, }(window, document, RSVP, rJS,
XMLHttpRequest, location, console, navigator, Event, URL, domsugar)); XMLHttpRequest, location, console, navigator, Event, URL, domsugar,
convertOriginalErrorToErrorDataList, buildErrorElementFromErrorText
));
...@@ -236,7 +236,7 @@ ...@@ -236,7 +236,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1005.39881.44610.14899</string> </value> <value> <string>1007.4476.40395.38075</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -256,7 +256,7 @@ ...@@ -256,7 +256,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1673426576.41</float> <float>1678908218.84</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