feat: better axios errors handling

This commit is contained in:
Bobby Wibowo 2022-08-04 23:47:48 +07:00
parent c6c485447f
commit 8c26fa4ffa
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
5 changed files with 91 additions and 101 deletions

View File

@ -10,7 +10,20 @@ const page = {
// HTML elements // HTML elements
user: null, 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 = () => { page.unhide = () => {
@ -26,22 +39,7 @@ page.unhide = () => {
// Handler for Axios errors // Handler for Axios errors
page.onAxiosError = error => { page.onAxiosError = error => {
console.error(error) const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText
// 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 description = error.response.data && error.response.data.description const description = error.response.data && error.response.data.description
? error.response.data.description ? error.response.data.description
: 'There was an error with the request.\nPlease check the console for more information.' : 'There was an error with the request.\nPlease check the console for more information.'

View File

@ -108,7 +108,20 @@ const page = {
fadingIn: null, fadingIn: null,
albumTitleMaxLength: 70, 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 = () => { page.unhide = () => {
@ -134,22 +147,7 @@ page.onError = error => {
// Handler for Axios errors // Handler for Axios errors
page.onAxiosError = error => { page.onAxiosError = error => {
console.error(error) const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText
// 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 description = error.response.data && error.response.data.description const description = error.response.data && error.response.data.description
? error.response.data.description ? error.response.data.description
: 'There was an error with the request.\nPlease check the console for more information.' : '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.permissions = response.data.permissions
page.prepareDashboard() page.prepareDashboard()
}).catch(error => { }).catch(error => {
return swal({ return page.onAxiosError(error).then(() => {
title: 'An error occurred!',
text: error.response.data ? error.response.data.description : error.toString(),
icon: 'error'
}).then(() => {
if (error.response.data && error.response.data.code === 10001) { if (error.response.data && error.response.data.code === 10001) {
localStorage.removeItem(lsKeys.token) localStorage.removeItem(lsKeys.token)
window.location = 'auth' window.location = 'auth'

View File

@ -22,7 +22,20 @@ const page = {
titleFormat: null, titleFormat: null,
file: 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 => { page.updateMessageBody = content => {
@ -42,20 +55,7 @@ page.onError = error => {
// Handler for Axios errors // Handler for Axios errors
page.onAxiosError = error => { page.onAxiosError = error => {
// Better Cloudflare errors const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText
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 description = error.response.data && error.response.data.description const description = error.response.data && error.response.data.description
? error.response.data.description ? error.response.data.description

View File

@ -60,7 +60,20 @@ const page = {
videoExts: ['.avi', '.m2ts', '.m4v', '.mkv', '.mov', '.mp4', '.webm', '.wmv'], videoExts: ['.avi', '.m2ts', '.m4v', '.mkv', '.mov', '.mp4', '.webm', '.wmv'],
albumTitleMaxLength: 70, 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 // Handler for errors during initialization
@ -104,30 +117,19 @@ page.onError = error => {
} }
// Handler for Axios errors // Handler for Axios errors
page.onAxiosError = (error, cont) => { page.onAxiosError = error => {
if (!cont) console.error(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 return swal(`${error.response.status} ${statusText}`, description, 'error')
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 page.formatAxiosError = error => {
const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText
if (!cont) { if (error.response.data && error.response.data.description) {
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) {
return error.response return error.response
} else { } else {
const description = error.response const description = error.response
@ -223,11 +225,7 @@ page.verifyToken = token => {
return page.prepareUpload() return page.prepareUpload()
}).catch(error => { }).catch(error => {
return swal({ return page.onAxiosError(error).then(() => {
title: 'An error occurred!',
text: error.response.data ? error.response.data.description : error.toString(),
icon: 'error'
}).then(() => {
if (error.response.data && error.response.data.code === 10001) { if (error.response.data && error.response.data.code === 10001) {
localStorage.removeItem(lsKeys.token) localStorage.removeItem(lsKeys.token)
window.location.reload() window.location.reload()
@ -526,13 +524,13 @@ page.prepareDropzone = () => {
if (typeof error === 'object' && error.description) { if (typeof error === 'object' && error.description) {
err = error.description err = error.description
} else if (xhr) { } else if (xhr) {
// Formatting the Object is necessary since the function expect Axios errors const formatted = page.formatAxiosError({
err = page.onAxiosError({
response: { response: {
status: xhr.status, status: xhr.status,
statusText: xhr.statusText statusText: xhr.statusText
} }
}, true).data.description })
err = formatted.data.description
} else if (error instanceof Error) { } else if (error instanceof Error) {
err = error.toString() err = error.toString()
} }
@ -573,7 +571,7 @@ page.prepareDropzone = () => {
// strip tags cannot yet be configured per file with this API // strip tags cannot yet be configured per file with this API
striptags: page.stripTags 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') file.previewElement.querySelector('.descriptive-progress').classList.add('is-hidden')
if (response.data.success === false) { if (response.data.success === false) {
@ -667,7 +665,7 @@ page.processUrlsQueue = () => {
age: page.uploadAge, age: page.uploadAge,
filelength: page.fileLength filelength: page.fileLength
} }
}).catch(error => page.onAxiosError(error, true)).then(response => { }).catch(error => page.formatAxiosError(error)).then(response => {
return finishedUrlUpload(file, response.data) return finishedUrlUpload(file, response.data)
}) })
} }

View File

@ -14,7 +14,20 @@ const page = {
titleFormat: null, titleFormat: null,
videoContainer: document.querySelector('#playerContainer'), 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) // Disable video.js telemetry (should already be disabled by default since v7 though)
@ -38,20 +51,7 @@ page.onError = error => {
// Handler for Axios errors // Handler for Axios errors
page.onAxiosError = error => { page.onAxiosError = error => {
// Better Cloudflare errors const statusText = page.cloudflareErrors[error.response.status] || error.response.statusText
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 description = error.response.data && error.response.data.description const description = error.response.data && error.response.data.description
? error.response.data.description ? error.response.data.description