Better error handling with Mojang REST api.

Added handling for when there is no internet connection or Mojang's auth server is unreachable to both the login and status modules.
This commit is contained in:
Daniel Scalzi 2018-04-14 18:42:45 -04:00
parent b5386c0257
commit 66a3854a24
No known key found for this signature in database
GPG Key ID: 5CA2F145B63535F9
3 changed files with 74 additions and 24 deletions

View File

@ -62,8 +62,8 @@ document.addEventListener('readystatechange', function(){
// Update Mojang Status Color
const refreshMojangStatuses = async function(){
console.log('Refreshing Mojang Statuses..')
let status = 'grey'
try {
let status = 'grey'
const statuses = await Mojang.status()
greenCount = 0
for(let i=0; i<statuses.length; i++){
@ -79,12 +79,11 @@ document.addEventListener('readystatechange', function(){
if(greenCount == statuses.length){
status = 'green'
}
document.getElementById('mojang_status_icon').style.color = Mojang.statusToHex(status)
} catch (err) {
console.error('Unable to refresh Mojang service status..', err)
console.warn('Unable to refresh Mojang service status.')
console.debug(err)
}
document.getElementById('mojang_status_icon').style.color = Mojang.statusToHex(status)
}
refreshMojangStatuses()

View File

@ -75,7 +75,12 @@ exports.status = function(){
json: true
},
function(error, response, body){
if(response.statusCode === 200){
if(error || response.statusCode !== 200){
console.warn('Unable to retrieve Mojang status.')
console.debug('Error while retrieving Mojang statuses:', error)
reject(error || response.statusCode)
} else {
for(let i=0; i<body.length; i++){
const key = Object.keys(body[i])[0]
inner:
@ -87,8 +92,6 @@ exports.status = function(){
}
}
fulfill(statuses)
} else {
reject()
}
})
})
@ -119,10 +122,15 @@ exports.authenticate = function(username, password, clientToken, requestUser = t
}
},
function(error, response, body){
if(response.statusCode === 200){
fulfill(body)
if(error){
console.error('Error during authentication.', error)
reject(error)
} else {
reject(body)
if(response.statusCode === 200){
fulfill(body)
} else {
reject(body || {code: 'ENOTFOUND'})
}
}
})
})
@ -148,11 +156,16 @@ exports.validate = function(accessToken, clientToken){
}
},
function(error, response, body){
if(response.statusCode === 403){
fulfill(false)
if(error){
console.error('Error during validation.', error)
reject(error)
} else {
// 204 if valid
fulfill(true)
if(response.statusCode === 403){
fulfill(false)
} else {
// 204 if valid
fulfill(true)
}
}
})
})
@ -178,10 +191,15 @@ exports.invalidate = function(accessToken, clientToken){
}
},
function(error, response, body){
if(response.statusCode === 200){
fulfill()
if(error){
console.error('Error during invalidation.', error)
reject(error)
} else {
reject()
if(response.statusCode === 204){
fulfill()
} else {
reject(body)
}
}
})
})
@ -210,10 +228,15 @@ exports.refresh = function(accessToken, clientToken, requestUser = true){
}
},
function(error, response, body){
if(response.statusCode === 200){
fulfill(body)
if(error){
console.error('Error during refresh.', error)
reject(error)
} else {
reject(response.body)
if(response.statusCode === 200){
fulfill(body)
} else {
reject(body)
}
}
})
})

View File

@ -180,6 +180,8 @@
}
function resolveError(err){
// Mojang Response => err.cause | err.error | err.errorMessage
// Node error => err.code | err.message
if(err.cause != null && err.cause === 'UserMigratedException') {
return {
title: 'Error During Login:<br>Invalid Credentials',
@ -202,11 +204,37 @@
}
}
}
} else {
// Request errors (from Node).
if(err.code != null){
if(err.code === 'ENOENT'){
// No Internet.
return {
title: 'Error During Login:<br>No Internet Connection',
desc: 'You must be connected to the internet in order to login. Please connect and try again.'
}
} else if(err.code === 'ENOTFOUND'){
// Could not reach server.
return {
title: 'Error During Login:<br>Authentication Server Offline',
desc: 'Mojang\'s authentication server is currently offline or unreachable. Please wait a bit and try again. You can check the status of the server on <a href="https://help.mojang.com/">Mojang\'s help portal</a>.'
}
}
}
}
}
return {
title: err.error,
desc: err.errorMessage
if(err.message != null){
// Unknown error with request.
return {
title: 'Error During Login:<br>Unknown Error',
desc: err.message
}
} else {
// Unknown Mojang error.
return {
title: err.error,
desc: err.errorMessage
}
}
}