Skip to content

Commit

Permalink
fix: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tutinoko2048 committed Aug 15, 2024
1 parent fd09734 commit d90fb45
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
10 changes: 6 additions & 4 deletions src/updater/CacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@ export class CacheManager {
fs.mkdirSync(cacheFolder);
logger.info('Created cache folder');
}

this.load();
}

private save() {
fs.writeFileSync(cacheFile, JSON.stringify(this.cache, null, 2), 'utf8');
}

private load(): void {
public load(): void {
if (!fs.existsSync(cacheFile)) {
this.cache = {
license: false,
version: '0.0.0',
};
this.save();
} else {
this.cache = JSON.parse(fs.readFileSync(cacheFile, 'utf8'));
try {
this.cache = JSON.parse(fs.readFileSync(cacheFile, 'utf8'));
} catch (error) {
throw new Error(`Failed to load cache file, try deleting .launcher-cache/cache.json`);
}
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/updater/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ServerUpdater {
}

async run(): Promise<void> {
this.cacheManager.load();

const { shouldUpdate, latestVersion } = await this.checkUpdate();
if (shouldUpdate) {
this.logger.info(`New version available: ${latestVersion}`);
Expand Down Expand Up @@ -55,13 +57,10 @@ class ServerUpdater {
this.cacheManager.setVersion(versionInfo.version);
}

async checkLicense() {
async checkLicense(): Promise<void> {
if (this.cacheManager.getLicense()) return;
const result = await askLicense();
if (!result) {
this.logger.error('You must agree to the Minecraft EULA to use the server');
exit(1);
}
if (!result) throw new Error('\nYou must agree to the Minecraft EULA to use the server');
this.cacheManager.setLicense(true);
}

Expand All @@ -71,12 +70,14 @@ class ServerUpdater {
}
}

const isDebug = process.argv.includes('--debug');

const updater = new ServerUpdater();
try {
await updater.run();
exit();
} catch(err: any) {
updater.logger.error(`Failed to update server: ${err}`);
console.error(err?.stack)
updater.logger.error(`Error: ${err.message}`);
if (isDebug) console.error(err?.stack);
exit(1);
}

0 comments on commit d90fb45

Please sign in to comment.