diff --git a/assets/js/common/api.js b/assets/js/common/api.js index 835312ca..27200798 100644 --- a/assets/js/common/api.js +++ b/assets/js/common/api.js @@ -137,9 +137,30 @@ ComunicWeb.common.api = { }, }; } - else - //Prepare result - var result = JSON.parse(apiXHR.responseText); + else { + + //Catch JSON parsing errors + try { + + //Parse result + 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 if(result.error){ diff --git a/assets/js/common/errors.js b/assets/js/common/errors.js index 01bcad87..1507f46c 100644 --- a/assets/js/common/errors.js +++ b/assets/js/common/errors.js @@ -14,7 +14,7 @@ */ ComunicWeb.common.error.submitError = function(errorLevel, errorMessage, errorCode, errorData){ //Prepare API request - var apiURI = "webApp/reportError"; + /*var apiURI = "webApp/reportError"; var params = { "errorLevel": errorLevel, "errorMessage": errorMessage, @@ -27,7 +27,7 @@ ComunicWeb.common.error.submitError = function(errorLevel, errorMessage, errorCo nextAction = function(){}; //Send API request - ComunicWeb.common.api.makeAPIrequest(apiURI, params, requireLoginToken, nextAction); + ComunicWeb.common.api.makeAPIrequest(apiURI, params, requireLoginToken, nextAction);*/ } /** @@ -98,4 +98,75 @@ ComunicWeb.common.error.pageNotFound = function(additionnalData, targetElement){ //Everything seems to be OK 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: "File: "+error.fileName+" Line: " + error.lineNumber+ " Column: " + error.columnNumber + }); + + //Error message + createElem2({ + appendTo: errorContainer, + type: "p", + innerHTML: "Message: " + 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 + }); + } } \ No newline at end of file diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 0bef3aaa..5dd7a331 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -104,6 +104,11 @@ var ComunicWeb = { */ createLoadingCallout: function(target){}, + /** + * Create dialog skeleton + */ + createDialogSkeleton: function(info){}, + /** * Create and display a confirmation dialog */ @@ -133,6 +138,11 @@ var ComunicWeb = { * Handle a 404 not found error */ pageNotFound: function(additionnalData, targetElement){}, + + /** + * Handles and display SyntaxtError + */ + syntaxtError: function(error, additional){}, }, /** diff --git a/assets/js/common/messages.js b/assets/js/common/messages.js index 242fe311..035e315b 100644 --- a/assets/js/common/messages.js +++ b/assets/js/common/messages.js @@ -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 *