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
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.'

View File

@ -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'

View File

@ -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

View File

@ -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)
})
}

View File

@ -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