mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-30 22:34:10 -05:00
fix: 🐛 check error code to prevent data loss
This commit is contained in:
parent
a91a0d3bef
commit
93aae05e63
1 changed files with 22 additions and 3 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
const fs = require('fs');
|
||||
const Path = require('path');
|
||||
const logger = require('../../logger');
|
||||
|
||||
/**
|
||||
* Handle local database.
|
||||
|
@ -15,10 +16,28 @@ const Path = require('path');
|
|||
*/
|
||||
constructor(path) {
|
||||
this.path = path;
|
||||
let dbFile;
|
||||
|
||||
try {
|
||||
this.data = JSON.parse(fs.readFileSync(this.path, 'utf8'));
|
||||
} catch(_) {
|
||||
this.data = {list: []};
|
||||
dbFile = fs.readFileSync(this.path, 'utf8');
|
||||
} catch (err) {
|
||||
if (err.code === 'ENOENT') { // Only recreate if file not found to prevent data loss
|
||||
this.data = {list: []};
|
||||
} else {
|
||||
logger.logger.error(
|
||||
'Failed to read package database file, please check the error printed below and fix it manually:\n',
|
||||
`File Path: ${this.path}\n\n`,
|
||||
err
|
||||
);
|
||||
throw new Error('Package database file unreachable');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
this.data = JSON.parse(dbFile);
|
||||
} catch(err) {
|
||||
logger.logger.error(err);
|
||||
throw new Error(`Package database file corrupted (invalid JSON), please fix it manually.\nFile Path: ${this.path}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue