Skip to content

Commit

Permalink
feat: hibakezelés javított kommunikációval
Browse files Browse the repository at this point in the history
  • Loading branch information
juzraai committed Oct 14, 2019
1 parent f264353 commit 99dbdeb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
szamlak/
temp/
.env
.env
error.log
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env node
const SingleInstance = require('single-instance');
const { start } = require('./src/main');
const { handleError } = require('./src/err');

new SingleInstance('dijnet-bot').lock().then(start).catch(_ => {
console.log('A Díjnet bot már fut, és nem futtatható több példányban.');
new SingleInstance('dijnet-bot').lock().then(start).catch(error => {
handleError(error.stack ? error : 'A Díjnet bot már fut, és nem futtatható több példányban.');
});
20 changes: 20 additions & 0 deletions src/err.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs');
const chalk = require('chalk');

const fn = './error.log';

function printRed(message) {
console.log(chalk.red(message));
}

function handleError(error) {
if (error.message && error.stack) {
printRed(`\nHa biztos vagy abban, hogy a Díjnet felhasználóneved és jelszavad, valamint a konfigurációs paramétereket helyesen adtad meg, akkor a hiba a programban lehet.\n\nA hiba részleteit megtalálod az ${fn} fájlban. Kérlek, az alábbi linken nyiss egy új issue-t, másold be az ${fn} fájl tartalmát, és írd le röviden, milyen szituációban jelentkezett a hiba!\n\n--> https://github.com/juzraai/dijnet-bot/issues\n`);
fs.writeFileSync(fn, `${error.message}\n${error.stack}`);
} else {
printRed(error);
}
process.exit(1);
}

module.exports = { handleError };
11 changes: 7 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require('fs');
const path = require('path');
const mkdirp = require('util').promisify(require('mkdirp'));
require('./conf');
require('./conf'); // eslint-disable-line import/no-unassigned-import
const { handleError } = require('./err');
const dijnet = require('./lib');
const log = require('./logger');

Expand All @@ -12,7 +13,7 @@ function tmp(name) {
const alreadyCrawledIdsFile = path.join(process.env.OUTPUT_DIR, 'kesz.txt');
let alreadyCrawledIds = null;
function isAlreadyCrawled(id) {
if (null == alreadyCrawledIds) {
if (alreadyCrawledIds === null) {
if (fs.existsSync(alreadyCrawledIdsFile)) {
alreadyCrawledIds = fs.readFileSync(alreadyCrawledIdsFile, 'utf8').split('\n');
} else {
Expand Down Expand Up @@ -83,10 +84,12 @@ const start = async () => {
await dijnet.szamla_list(tmp(`szamla_list_${invoice.rowid}.html`));
}
log.success('Kész');

process.exit(0); // Windows 10-en Git Bash-ben különben nem áll le a program valamiért
} catch (error) {
log.error(error.message);
log.trace(error.stack);
process.exit(1);
log.error(error.message);
handleError(error);
}
};

Expand Down

0 comments on commit 99dbdeb

Please sign in to comment.