Catch JSON exceptions

This commit is contained in:
Pierre 2018-05-05 12:12:33 +02:00
parent 1e2637ab48
commit e54998128f
4 changed files with 194 additions and 5 deletions

View File

@ -137,10 +137,31 @@ ComunicWeb.common.api = {
}, },
}; };
} }
else else {
//Prepare result
//Catch JSON parsing errors
try {
//Parse result
var result = JSON.parse(apiXHR.responseText); var result = JSON.parse(apiXHR.responseText);
} catch (error) {
//Report error
ComunicWeb.common.error.syntaxtError(error, apiXHR.responseText);
//Set arbitray result content
result = {
error : {
code: 1,
message: "Invalid response",
},
};
}
}
//We check if we got any error //We check if we got any error
if(result.error){ if(result.error){
//Log error //Log error

View File

@ -14,7 +14,7 @@
*/ */
ComunicWeb.common.error.submitError = function(errorLevel, errorMessage, errorCode, errorData){ ComunicWeb.common.error.submitError = function(errorLevel, errorMessage, errorCode, errorData){
//Prepare API request //Prepare API request
var apiURI = "webApp/reportError"; /*var apiURI = "webApp/reportError";
var params = { var params = {
"errorLevel": errorLevel, "errorLevel": errorLevel,
"errorMessage": errorMessage, "errorMessage": errorMessage,
@ -27,7 +27,7 @@ ComunicWeb.common.error.submitError = function(errorLevel, errorMessage, errorCo
nextAction = function(){}; nextAction = function(){};
//Send API request //Send API request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, requireLoginToken, nextAction); ComunicWeb.common.api.makeAPIrequest(apiURI, params, requireLoginToken, nextAction);*/
} }
/** /**
@ -99,3 +99,74 @@ ComunicWeb.common.error.pageNotFound = function(additionnalData, targetElement){
//Everything seems to be OK //Everything seems to be OK
return true; return true;
} }
/**
* Handles and display SyntaxtError
*
* @param {SyntaxError} error The error
* @param {string} additional Additionnal information to include with
* the report
*/
ComunicWeb.common.error.syntaxtError = function(error, additional){
//Create a modal dialog to report error
var dialog = ComunicWeb.common.messages.createDialogSkeleton({
type: "danger",
title: "An error occurred"
});
//Display modal
$(dialog.modal).modal("show");
//Create close modal function
var closeModal = function(){
$(dialog.modal).modal('hide');
emptyElem(dialog.modal);
dialog.modal.remove();
}
dialog.closeModal.onclick = closeModal;
dialog.cancelButton.onclick = closeModal;
//Create error container
var errorContainer = createElem2({
appendTo: dialog.modalBody,
type: "div"
});
//Message
createElem2({
appendTo: errorContainer,
type: "p",
innerHTML: "An error has just occured. There are the details of the error. If this error occurs several times, please inform us at contact@communiquons.org ."
});
//Locate error
createElem2({
appendTo: errorContainer,
type: "p",
innerHTML: "<b>File:</b> "+error.fileName+" <b>Line:</b> " + error.lineNumber+ " <b>Column:</b> " + error.columnNumber
});
//Error message
createElem2({
appendTo: errorContainer,
type: "p",
innerHTML: "<b>Message:</b> " + error.message,
});
//Stack trace
createElem2({
appendTo: errorContainer,
type: "pre",
innerHTML: error.stack
});
//Check for additional information
if(additional){
createElem2({
appendTo: errorContainer,
type: "pre",
innerHTML: "Additionnal information: \n " + additional
});
}
}

View File

@ -104,6 +104,11 @@ var ComunicWeb = {
*/ */
createLoadingCallout: function(target){}, createLoadingCallout: function(target){},
/**
* Create dialog skeleton
*/
createDialogSkeleton: function(info){},
/** /**
* Create and display a confirmation dialog * Create and display a confirmation dialog
*/ */
@ -133,6 +138,11 @@ var ComunicWeb = {
* Handle a 404 not found error * Handle a 404 not found error
*/ */
pageNotFound: function(additionnalData, targetElement){}, pageNotFound: function(additionnalData, targetElement){},
/**
* Handles and display SyntaxtError
*/
syntaxtError: function(error, additional){},
}, },
/** /**

View File

@ -57,6 +57,93 @@ ComunicWeb.common.messages.createLoadingCallout = function(target){
} }
/**
* Create dialog skeleton
*
* @param {object} info Information about the callout to create
* @argument {string} type The type of modal
* @param {string} title The title of the modal
* @return {object} Information about the created dialog
*/
ComunicWeb.common.messages.createDialogSkeleton = function(info){
data = {};
//Get modal type
var modalType = info.type ? info.type : "default";
//Get modal title
var modalTitle = info.title ? info.title : "";
//Create a modal root
data.modal = createElem2({
type: "div",
class: "modal modal-" + modalType
});
var modalDialog = createElem2({
appendTo: data.modal,
type: "div",
class: "modal-dialog"
});
data.modalContent = createElem2({
appendTo: modalDialog,
type: "div",
class: "modal-content",
});
//Modal header
data.modalHeader = createElem2({
appendTo: data.modalContent,
type: "div",
class: "modal-header"
});
data.closeModal = createElem2({
appendTo: data.modalHeader,
type: "button",
class: "close",
});
createElem2({
appendTo: data.closeModal,
type: "span",
innerHTML: "x"
});
//Modal title
data.modalTitle = createElem2({
appendTo: data.modalHeader,
type: "h4",
class: "modal-title",
innerHTML: modalTitle
});
//Modal body
data.modalBody = createElem2({
appendTo: data.modalContent,
type: "div",
class: "modal-body",
});
//Modal footer
data.modalFooter = createElem2({
appendTo: data.modalContent,
type: "div",
class: "modal-footer"
});
data.cancelButton = createElem2({
appendTo: data.modalFooter,
type: "button",
class: "btn btn-default",
innerHTML: "Cancel"
});
return data;
}
/** /**
* Create a confirmation dialog * Create a confirmation dialog
* *