Skip to content

Commit

Permalink
refactor: 解释 JSON 失败时使用空数组
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Nov 9, 2024
1 parent 8d988ab commit 708d0c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/statistics/ClusterStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ export class StatsStorage {
}

private loadFromFile(): void {
// 直接读取 JSON 数据
const fileContent = fs.readFileSync(this.filePath, 'utf8');
this.data = JSON.parse(fileContent);
try {
const fileContent = fs.readFileSync(this.filePath, 'utf8');
this.data = JSON.parse(fileContent);
}
catch (e) {
this.data = [];
}
}

public stopAutoSave(): void {
Expand Down
9 changes: 7 additions & 2 deletions src/statistics/HourlyStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ export class HourlyStatsStorage {
}

private loadFromFile(): void {
const fileContent = fs.readFileSync(this.filePath, 'utf8');
this.data = JSON.parse(fileContent);
try {
const fileContent = fs.readFileSync(this.filePath, 'utf8');
this.data = JSON.parse(fileContent);
}
catch (e) {
this.data = [];
}
}

public stopAutoSave(): void {
Expand Down
9 changes: 7 additions & 2 deletions src/statistics/NumberStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ export class NumberStorage {
}

private loadFromFile(): void {
const data = fs.readFileSync(this.filePath, 'utf8');
this.data = JSON.parse(data);
try {
const data = fs.readFileSync(this.filePath, 'utf8');
this.data = JSON.parse(data);
}
catch (e) {
this.data = [];
}
}

public stopAutoSave(): void {
Expand Down

0 comments on commit 708d0c2

Please sign in to comment.