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,
ensureArray, Array*/
ensureArray, Array, domsugar, Event, XMLHttpRequest, navigator*/
/*jslint indent: 2, maxerr: 3, nomen: true, unparam: true, continue: true */
(function (window, RSVP, SimpleQuery, ComplexQuery, Query,
ensureArray, Array) {
ensureArray, Array, domsugar, Event, XMLHttpRequest, navigator) {
"use strict";
///////////////////////////////
......@@ -292,6 +292,122 @@
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() {
var action_list = ensureArray(this.state.erp5_document._links.action_object_list_action || []),
i,
......@@ -441,7 +557,10 @@
}
window.mergeGlobalActionWithRawActionList = mergeGlobalActionWithRawActionList;
window.triggerListboxClipboardAction = triggerListboxClipboardAction;
window.convertOriginalErrorToErrorDataList = convertOriginalErrorToErrorDataList;
window.buildErrorElementFromErrorText = buildErrorElementFromErrorText;
window.declareGadgetClassCanHandleListboxClipboardAction =
declareGadgetClassCanHandleListboxClipboardAction;
}(window, RSVP, SimpleQuery, ComplexQuery, Query, ensureArray, Array));
\ No newline at end of file
}(window, RSVP, SimpleQuery, ComplexQuery, Query, ensureArray, Array, domsugar,
Event, XMLHttpRequest, navigator));
......@@ -236,7 +236,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>991.63236.62412.49203</string> </value>
<value> <string>1007.4418.49274.62395</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -256,7 +256,7 @@
</tuple>
<state>
<tuple>
<float>1620739824.54</float>
<float>1678908170.99</float>
<string>UTC</string>
</tuple>
</state>
......
/*globals window, document, RSVP, rJS,
URI, location, XMLHttpRequest, console, navigator, Event,
URL, domsugar*/
URL, domsugar, convertOriginalErrorToErrorDataList,
buildErrorElementFromErrorText*/
/*jslint indent: 2, maxlen: 80, unparam: true*/
(function (window, document, RSVP, rJS,
XMLHttpRequest, location, console, navigator, Event,
URL, domsugar) {
URL, domsugar, convertOriginalErrorToErrorDataList,
buildErrorElementFromErrorText) {
"use strict";
var MAIN_SCOPE = "m",
......@@ -130,97 +132,36 @@
}
function displayErrorContent(gadget, original_error) {
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;
return convertOriginalErrorToErrorDataList(original_error)
.push(function (error_list) {
var error = error_list[0],
error_text = error_list[1];
console.error(original_error);
if (original_error instanceof Error) {
console.error(original_error.stack);
}
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) {
if (gadget.props === undefined) {
// Gadget has not yet been correctly initialized
throw error;
}
}
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);
if (original_error instanceof Error) {
console.error(original_error.stack);
}
if (gadget.props === undefined) {
// Gadget has not yet been correctly initialized
throw error;
}
if (error_response && error_response.text) {
return error_response.text().then(
function (request_error_text) {
return gadget.changeState({
error_text: error_text,
request_error_text: request_error_text,
url: undefined
});
if (error.response && error.response.text) {
return error.response.text().then(
function (request_error_text) {
return gadget.changeState({
error_text: error_text,
request_error_text: request_error_text,
url: undefined
});
}
);
}
);
}
return gadget.changeState({
error_text: error_text,
request_error_text: error_response,
url: undefined
});
return gadget.changeState({
error_text: error_text,
request_error_text: error.response,
url: undefined
});
});
}
function displayError(gadget, error) {
......@@ -788,44 +729,13 @@
return;
})
.push(function () {
var element = gadget.props.content_element,
container;
return buildErrorElementFromErrorText(
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
while (element.firstChild) {
......@@ -1068,4 +978,6 @@
}(window, document, RSVP, rJS,
XMLHttpRequest, location, console, navigator, Event, URL, domsugar));
XMLHttpRequest, location, console, navigator, Event, URL, domsugar,
convertOriginalErrorToErrorDataList, buildErrorElementFromErrorText
));
......@@ -236,7 +236,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1005.39881.44610.14899</string> </value>
<value> <string>1007.4476.40395.38075</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -256,7 +256,7 @@
</tuple>
<state>
<tuple>
<float>1673426576.41</float>
<float>1678908218.84</float>
<string>UTC</string>
</tuple>
</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