diff --git a/src/js/auth.js b/src/js/auth.js index cabccbd..c493ecc 100644 --- a/src/js/auth.js +++ b/src/js/auth.js @@ -10,7 +10,20 @@ const page = { // HTML elements user: null, - pass: null + pass: null, + + // Better Cloudflare errors + cloudflareErrors: { + 520: 'Unknown Error', + 521: 'Web Server Is Down', + 522: 'Connection Timed Out', + 523: 'Origin Is Unreachable', + 524: 'A Timeout Occurred', + 525: 'SSL Handshake Failed', + 526: 'Invalid SSL Certificate', + 527: 'Railgun Error', + 530: 'Origin DNS Error' + } } page.unhide = () => { @@ -26,22 +39,7 @@ page.unhide = () => { // Handler for Axios errors page.onAxiosError = error => { - console.error(error) - - // Better Cloudflare errors - const cloudflareErrors = { - 520: 'Unknown Error', - 521: 'Web Server Is Down', - 522: 'Connection Timed Out', - 523: 'Origin Is Unreachable', - 524: 'A Timeout Occurred', - 525: 'SSL Handshake Failed', - 526: 'Invalid SSL Certificate', - 527: 'Railgun Error', - 530: 'Origin DNS Error' - } - - const statusText = cloudflareErrors[error.response.status] || error.response.statusText + const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText const description = error.response.data && error.response.data.description ? error.response.data.description : 'There was an error with the request.\nPlease check the console for more information.' diff --git a/src/js/dashboard.js b/src/js/dashboard.js index 814e8c8..e20dab1 100644 --- a/src/js/dashboard.js +++ b/src/js/dashboard.js @@ -108,7 +108,20 @@ const page = { fadingIn: null, albumTitleMaxLength: 70, - albumDescMaxLength: 4000 + albumDescMaxLength: 4000, + + // Better Cloudflare errors + cloudflareErrors: { + 520: 'Unknown Error', + 521: 'Web Server Is Down', + 522: 'Connection Timed Out', + 523: 'Origin Is Unreachable', + 524: 'A Timeout Occurred', + 525: 'SSL Handshake Failed', + 526: 'Invalid SSL Certificate', + 527: 'Railgun Error', + 530: 'Origin DNS Error' + } } page.unhide = () => { @@ -134,22 +147,7 @@ page.onError = error => { // Handler for Axios errors page.onAxiosError = error => { - console.error(error) - - // Better Cloudflare errors - const cloudflareErrors = { - 520: 'Unknown Error', - 521: 'Web Server Is Down', - 522: 'Connection Timed Out', - 523: 'Origin Is Unreachable', - 524: 'A Timeout Occurred', - 525: 'SSL Handshake Failed', - 526: 'Invalid SSL Certificate', - 527: 'Railgun Error', - 530: 'Origin DNS Error' - } - - const statusText = cloudflareErrors[error.response.status] || error.response.statusText + const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText const description = error.response.data && error.response.data.description ? error.response.data.description : 'There was an error with the request.\nPlease check the console for more information.' @@ -196,11 +194,7 @@ page.verifyToken = token => { page.permissions = response.data.permissions page.prepareDashboard() }).catch(error => { - return swal({ - title: 'An error occurred!', - text: error.response.data ? error.response.data.description : error.toString(), - icon: 'error' - }).then(() => { + return page.onAxiosError(error).then(() => { if (error.response.data && error.response.data.code === 10001) { localStorage.removeItem(lsKeys.token) window.location = 'auth' diff --git a/src/js/file.js b/src/js/file.js index 17547ce..84e2268 100644 --- a/src/js/file.js +++ b/src/js/file.js @@ -22,7 +22,20 @@ const page = { titleFormat: null, file: null, - clipboardJS: null + clipboardJS: null, + + // Better Cloudflare errors + cloudflareErrors: { + 520: 'Unknown Error', + 521: 'Web Server Is Down', + 522: 'Connection Timed Out', + 523: 'Origin Is Unreachable', + 524: 'A Timeout Occurred', + 525: 'SSL Handshake Failed', + 526: 'Invalid SSL Certificate', + 527: 'Railgun Error', + 530: 'Origin DNS Error' + } } page.updateMessageBody = content => { @@ -42,20 +55,7 @@ page.onError = error => { // Handler for Axios errors page.onAxiosError = error => { - // Better Cloudflare errors - const cloudflareErrors = { - 520: 'Unknown Error', - 521: 'Web Server Is Down', - 522: 'Connection Timed Out', - 523: 'Origin Is Unreachable', - 524: 'A Timeout Occurred', - 525: 'SSL Handshake Failed', - 526: 'Invalid SSL Certificate', - 527: 'Railgun Error', - 530: 'Origin DNS Error' - } - - const statusText = cloudflareErrors[error.response.status] || error.response.statusText + const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText const description = error.response.data && error.response.data.description ? error.response.data.description diff --git a/src/js/home.js b/src/js/home.js index 9d8b9ae..c1fe400 100644 --- a/src/js/home.js +++ b/src/js/home.js @@ -60,7 +60,20 @@ const page = { videoExts: ['.avi', '.m2ts', '.m4v', '.mkv', '.mov', '.mp4', '.webm', '.wmv'], albumTitleMaxLength: 70, - albumDescMaxLength: 4000 + albumDescMaxLength: 4000, + + // Better Cloudflare errors + cloudflareErrors: { + 520: 'Unknown Error', + 521: 'Web Server Is Down', + 522: 'Connection Timed Out', + 523: 'Origin Is Unreachable', + 524: 'A Timeout Occurred', + 525: 'SSL Handshake Failed', + 526: 'Invalid SSL Certificate', + 527: 'Railgun Error', + 530: 'Origin DNS Error' + } } // Handler for errors during initialization @@ -104,30 +117,19 @@ page.onError = error => { } // Handler for Axios errors -page.onAxiosError = (error, cont) => { - if (!cont) console.error(error) +page.onAxiosError = error => { + const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText + const description = error.response.data && error.response.data.description + ? error.response.data.description + : 'There was an error with the request.\nPlease check the console for more information.' - // Better Cloudflare errors - const cloudflareErrors = { - 520: 'Unknown Error', - 521: 'Web Server Is Down', - 522: 'Connection Timed Out', - 523: 'Origin Is Unreachable', - 524: 'A Timeout Occurred', - 525: 'SSL Handshake Failed', - 526: 'Invalid SSL Certificate', - 527: 'Railgun Error', - 530: 'Origin DNS Error' - } + return swal(`${error.response.status} ${statusText}`, description, 'error') +} - const statusText = cloudflareErrors[error.response.status] || error.response.statusText +page.formatAxiosError = error => { + const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText - if (!cont) { - const description = error.response.data && error.response.data.description - ? error.response.data.description - : 'There was an error with the request.\nPlease check the console for more information.' - return swal(`${error.response.status} ${statusText}`, description, 'error') - } else if (error.response.data && error.response.data.description) { + if (error.response.data && error.response.data.description) { return error.response } else { const description = error.response @@ -223,11 +225,7 @@ page.verifyToken = token => { return page.prepareUpload() }).catch(error => { - return swal({ - title: 'An error occurred!', - text: error.response.data ? error.response.data.description : error.toString(), - icon: 'error' - }).then(() => { + return page.onAxiosError(error).then(() => { if (error.response.data && error.response.data.code === 10001) { localStorage.removeItem(lsKeys.token) window.location.reload() @@ -526,13 +524,13 @@ page.prepareDropzone = () => { if (typeof error === 'object' && error.description) { err = error.description } else if (xhr) { - // Formatting the Object is necessary since the function expect Axios errors - err = page.onAxiosError({ + const formatted = page.formatAxiosError({ response: { status: xhr.status, statusText: xhr.statusText } - }, true).data.description + }) + err = formatted.data.description } else if (error instanceof Error) { err = error.toString() } @@ -573,7 +571,7 @@ page.prepareDropzone = () => { // strip tags cannot yet be configured per file with this API striptags: page.stripTags } - }).catch(error => page.onAxiosError(error, true)).then(response => { + }).catch(error => page.formatAxiosError(error)).then(response => { file.previewElement.querySelector('.descriptive-progress').classList.add('is-hidden') if (response.data.success === false) { @@ -667,7 +665,7 @@ page.processUrlsQueue = () => { age: page.uploadAge, filelength: page.fileLength } - }).catch(error => page.onAxiosError(error, true)).then(response => { + }).catch(error => page.formatAxiosError(error)).then(response => { return finishedUrlUpload(file, response.data) }) } diff --git a/src/js/player.js b/src/js/player.js index b78fe95..b84e549 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -14,7 +14,20 @@ const page = { titleFormat: null, videoContainer: document.querySelector('#playerContainer'), - player: null + player: null, + + // Better Cloudflare errors + cloudflareErrors: { + 520: 'Unknown Error', + 521: 'Web Server Is Down', + 522: 'Connection Timed Out', + 523: 'Origin Is Unreachable', + 524: 'A Timeout Occurred', + 525: 'SSL Handshake Failed', + 526: 'Invalid SSL Certificate', + 527: 'Railgun Error', + 530: 'Origin DNS Error' + } } // Disable video.js telemetry (should already be disabled by default since v7 though) @@ -38,20 +51,7 @@ page.onError = error => { // Handler for Axios errors page.onAxiosError = error => { - // Better Cloudflare errors - const cloudflareErrors = { - 520: 'Unknown Error', - 521: 'Web Server Is Down', - 522: 'Connection Timed Out', - 523: 'Origin Is Unreachable', - 524: 'A Timeout Occurred', - 525: 'SSL Handshake Failed', - 526: 'Invalid SSL Certificate', - 527: 'Railgun Error', - 530: 'Origin DNS Error' - } - - const statusText = cloudflareErrors[error.response.status] || error.response.statusText + const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText const description = error.response.data && error.response.data.description ? error.response.data.description