Should show error screen on error (#350)

* Should show error screen on error

* not sure how its there ;

* remove bedrock files as doesnt seem to be supported anyway

* lint fix

* handle non-promise variant as well
This commit is contained in:
Vitaly 2023-09-16 08:30:52 +03:00 committed by GitHub
parent 856639d586
commit 2fba863d66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 2 deletions

View file

@ -176,6 +176,17 @@ async function connect (options) {
loadingScreen.status = 'Logging in'
const errorAbortController = new AbortController()
window.addEventListener('unhandledrejection', (e) => {
handleError(e.reason)
}, {
signal: errorAbortController
})
window.addEventListener('error', (e) => {
handleError(e.message)
}, {
signal: errorAbortController.signal
})
const bot = mineflayer.createBot({
host,
port,
@ -189,12 +200,13 @@ async function connect (options) {
})
hud.preload(bot)
bot.on('error', (err) => {
const handleError = (err) => {
console.log('Encountered error!', err)
loadingScreen.status = `Error encountered. Error message: ${err}. Please reload the page`
loadingScreen.style = 'display: block;'
loadingScreen.hasError = true
})
}
bot.on('error', handleError)
bot.on('kicked', (kickReason) => {
console.log('User was kicked!', kickReason)
@ -398,6 +410,9 @@ async function connect (options) {
hud.style.display = 'block'
setTimeout(function () {
// game in playable state show errors in console instead
errorAbortController.abort()
if (loadingScreen.hasError) return
// remove loading screen, wait a second to make sure a frame has properly rendered
loadingScreen.style = 'display: none;'
}, 2500)

View file

@ -4,6 +4,7 @@
"description": "A minecraft client running in a browser",
"main": "index.js",
"scripts": {
"postinstall": "node scripts/patchPackages.js",
"build": "webpack --config webpack.prod.js",
"build-dev": "webpack --config webpack.dev.js",
"start": "node --max-old-space-size=8192 server.js 8080 dev",

25
scripts/patchPackages.js Normal file
View file

@ -0,0 +1,25 @@
// @ts-check
const path = require('path')
const dataPath = path.join(require.resolve('minecraft-data'), '../data.js')
const fs = require('fs')
const lines = fs.readFileSync(dataPath, 'utf8').split('\n')
if (lines[0] === '//patched') {
console.log('Already patched')
process.exit(0)
}
function removeLinesBetween (start, end) {
const startIndex = lines.findIndex(line => line === start)
if (startIndex === -1) return
const endIndex = startIndex + lines.slice(startIndex).findIndex(line => line === end)
// insert block comments
lines.splice(startIndex, 0, '/*')
lines.splice(endIndex + 2, 0, '*/')
}
removeLinesBetween(" 'bedrock': {", ' }')
lines.unshift('//patched')
fs.writeFileSync(dataPath, lines.join('\n'), 'utf8')