Skip to content

Commit

Permalink
json storage - better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico105 committed Sep 6, 2021
1 parent 513371d commit 58eafe9
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,19 @@ class GiveawaysManager extends EventEmitter {
return [];
} else {
// If the file exists, read it
const storageContent = await readFile(this.options.storage);
const storageContent = await readFile(this.options.storage, { encoding: 'utf-8'});
if (!storageContent.trim().startsWith('[') || !storageContent.trim().endsWith(']')) {
console.log(storageContent);
throw new SyntaxError('The storage file is not properly formatted (does not contain an array).');
}

try {
const giveaways = await JSON.parse(storageContent.toString(), (_, v) => (typeof v === 'string' && /BigInt\("(-?\d+)"\)/.test(v)) ? eval(v) : v);
if (Array.isArray(giveaways)) return giveaways;
else {
console.log(storageContent, giveaways);
throw new SyntaxError('The storage file is not properly formatted (giveaways is not an array).');
}
return await JSON.parse(storageContent, (_, v) => (typeof v === 'string' && /BigInt\("(-?\d+)"\)/.test(v)) ? eval(v) : v);
} catch (err) {
if (err.message === 'Unexpected end of JSON input') {
throw new SyntaxError('The storage file is not properly formatted (Unexpected end of JSON input).');
} else throw err;
if (err.message.startsWith('Unexpected token')) {
throw new SyntaxError(`${err.message} | LINK: (${require('path').resolve(this.options.storage)}:1:${err.message.split(' ').at(-1)})`);
}
throw err;
}
}
}
Expand Down

0 comments on commit 58eafe9

Please sign in to comment.