Skip to content

Commit

Permalink
Merge pull request #100 from architecture-building-systems/3593-bugfixes
Browse files Browse the repository at this point in the history
3593 bugfixes
  • Loading branch information
reyery committed May 28, 2024
2 parents 225586f + a9f4b5b commit 8d0f008
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
23 changes: 19 additions & 4 deletions electron/download.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import { createWriteStream, existsSync } from 'fs';
import { dirname } from 'path';
import { mkdir } from 'fs/promises';

const fetchWithRetry = async (url, options = {}, retries = 3, delay = 1000) => {
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error. Status: ${response.status}`);
}
return response;
} catch (error) {
if (retries > 0) {
console.debug(`Retrying... (${retries} retries left)`);
await new Promise((resolve) => setTimeout(resolve, delay));
return fetchWithRetry(url, options, retries - 1, delay);
} else {
throw new Error(`Failed to fetch ${url}: ${error.message}`);
}
}
};

export const downloadFile = async (url, dest) => {
// Ensure directory exists
const dir = dirname(dest);
Expand All @@ -13,12 +31,9 @@ export const downloadFile = async (url, dest) => {
const stream = createWriteStream(dest, { flags: 'w' });

try {
const { body, status } = await fetch(url);
if (status >= 400) throw new Error(`Unable to download file at ${url}`);

const { body } = await fetchWithRetry(url);
await finished(Readable.fromWeb(body).pipe(stream));
} catch (error) {
console.error(error);
stream.close();
throw error;
}
Expand Down
6 changes: 6 additions & 0 deletions electron/log.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { shell } from 'electron';
import log from 'electron-log';

export const initLog = () => {
Expand All @@ -10,3 +11,8 @@ export const initLog = () => {

return log;
};

export const openLog = () => {
const logFilePath = log.transports.file.getFile().path;
shell.openPath(logFilePath);
};
12 changes: 8 additions & 4 deletions electron/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
updateCEAenv,
} from './cea/env.mjs';
import { CEAError } from './cea/errors.mjs';
import { initLog } from './log.mjs';
import { initLog, openLog } from './log.mjs';
import { readConfig, writeConfig } from './config.mjs';

const __filename = fileURLToPath(import.meta.url);
Expand Down Expand Up @@ -288,13 +288,17 @@ function createSplashWindow(url) {
try {
await preflightChecks();
} catch (error) {
console.error(error);
dialog.showMessageBoxSync(splashWindow, {
type: 'error',
title: 'CEA Error',
message: 'CEA has encounted an error on startup',
detail: error.toString(),
buttons: ['Exit CEA'],
message:
'CEA has encounted an error on startup.\n The application will exit now.',
detail:
'You can report this error to us at our GitHub page\n (https://github.com/architecture-building-systems/CityEnergyAnalyst/issues).',
buttons: ['Show logs'],
});
openLog();
app.exit();
}
});
Expand Down

0 comments on commit 8d0f008

Please sign in to comment.