diff --git a/assets/js/error_handler.js b/assets/js/error_handler.js index 115e5907..8cf878c4 100644 --- a/assets/js/error_handler.js +++ b/assets/js/error_handler.js @@ -40,21 +40,6 @@ class ErrorHandlerHelper { _showAlert(statusText, statusCode, location, responseHTML) { const httpStatusToText = { - '200': 'OK', - '201': 'Created', - '202': 'Accepted', - '203': 'Non-Authoritative Information', - '204': 'No Content', - '205': 'Reset Content', - '206': 'Partial Content', - '300': 'Multiple Choices', - '301': 'Moved Permanently', - '302': 'Found', - '303': 'See Other', - '304': 'Not Modified', - '305': 'Use Proxy', - '306': 'Unused', - '307': 'Temporary Redirect', '400': 'Bad Request', '401': 'Unauthorized', '402': 'Payment Required', @@ -83,42 +68,67 @@ class ErrorHandlerHelper { '505': 'HTTP Version Not Supported', }; - //If the statusText is empty, we use the status code as text - if (!statusText) { - statusText = httpStatusToText[statusCode]; - } - - //Create error text - const title = statusText + ' (Status ' + statusCode + ')'; - - let trimString = function (string, length) { - return string.length > length ? - string.substring(0, length) + '...' : - string; + const userFriendlyMessages = { + '400': 'The request was invalid or malformed.', + '401': 'You need to log in to access this resource.', + '403': 'You don\'t have permission to access this resource.', + '404': 'The requested page or resource could not be found.', + '408': 'The request timed out. Please check your connection and try again.', + '409': 'There was a conflict with the current state of the resource.', + '429': 'Too many requests sent. Please wait a moment and try again.', + '500': 'An internal server error occurred. This is not your fault.', + '502': 'The server received an invalid response from an upstream service.', + '503': 'The service is temporarily unavailable. Please try again later.', + '504': 'The server did not respond in time. Please try again later.', }; - const short_location = trimString(location, 50); + if (!statusText) { + statusText = httpStatusToText[String(statusCode)] ?? 'Unknown Error'; + } - let url = location; - let msg = `Error calling ${short_location}.
`; - msg += 'Try to reload the page or contact the administrator if this error persists.'; - msg += '

View details'; - msg += "
"; + const title = `${statusText} (HTTP ${statusCode})`; + const friendlyMsg = userFriendlyMessages[String(statusCode)] + ?? 'An unexpected error occurred. Please try again or contact the administrator.'; + + const short_location = location.length > 80 + ? location.substring(0, 80) + '…' + : location; + + const msg = ` +

${friendlyMsg}

+

If this error keeps happening, please contact your administrator.

+ +
+ +
`; + + const footer = `Error while loading: ${short_location}`; Swal.fire({ + icon: 'error', title: title, html: msg, - width: '800px', + footer: footer, + width: '90%', + confirmButtonText: 'Reload page', + showCancelButton: true, + cancelButtonText: 'Close', + showCloseButton: true, + reverseButtons: true, didOpen: () => { - var dstFrame = document.getElementById('error-iframe'); + const dstFrame = document.getElementById('error-iframe'); //@ts-ignore - var dstDoc = dstFrame.contentDocument || dstFrame.contentWindow.document; + const dstDoc = dstFrame.contentDocument || dstFrame.contentWindow.document; dstDoc.write(responseHTML); dstDoc.close(); }, - }).then(() => { - //Remove blur + }).then((result) => { $('#content').removeClass('loading-content'); + if (result.isConfirmed) { + window.location.reload(); + } }); }