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 // Update Mojang Status Color
const refreshMojangStatuses = async function(){ const refreshMojangStatuses = async function(){
console.log('Refreshing Mojang Statuses..') console.log('Refreshing Mojang Statuses..')
try {
let status = 'grey' let status = 'grey'
try {
const statuses = await Mojang.status() const statuses = await Mojang.status()
greenCount = 0 greenCount = 0
for(let i=0; i<statuses.length; i++){ for(let i=0; i<statuses.length; i++){
@ -79,12 +79,11 @@ document.addEventListener('readystatechange', function(){
if(greenCount == statuses.length){ if(greenCount == statuses.length){
status = 'green' status = 'green'
} }
document.getElementById('mojang_status_icon').style.color = Mojang.statusToHex(status)
} catch (err) { } 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() refreshMojangStatuses()

View File

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

View File

@ -180,6 +180,8 @@
} }
function resolveError(err){ function resolveError(err){
// Mojang Response => err.cause | err.error | err.errorMessage
// Node error => err.code | err.message
if(err.cause != null && err.cause === 'UserMigratedException') { if(err.cause != null && err.cause === 'UserMigratedException') {
return { return {
title: 'Error During Login:<br>Invalid Credentials', title: 'Error During Login:<br>Invalid Credentials',
@ -202,13 +204,39 @@
} }
} }
} }
} 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>.'
} }
} }
}
}
}
if(err.message != null){
// Unknown error with request.
return {
title: 'Error During Login:<br>Unknown Error',
desc: err.message
}
} else {
// Unknown Mojang error.
return { return {
title: err.error, title: err.error,
desc: err.errorMessage desc: err.errorMessage
} }
} }
}
loginButton.addEventListener('click', () => { loginButton.addEventListener('click', () => {
// Disable form. // Disable form.