Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Commit 708d0c2

Browse files
committed
refactor: 解释 JSON 失败时使用空数组
1 parent 8d988ab commit 708d0c2

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/statistics/ClusterStats.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ export class StatsStorage {
9696
}
9797

9898
private loadFromFile(): void {
99-
// 直接读取 JSON 数据
100-
const fileContent = fs.readFileSync(this.filePath, 'utf8');
101-
this.data = JSON.parse(fileContent);
99+
try {
100+
const fileContent = fs.readFileSync(this.filePath, 'utf8');
101+
this.data = JSON.parse(fileContent);
102+
}
103+
catch (e) {
104+
this.data = [];
105+
}
102106
}
103107

104108
public stopAutoSave(): void {

src/statistics/HourlyStats.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@ export class HourlyStatsStorage {
111111
}
112112

113113
private loadFromFile(): void {
114-
const fileContent = fs.readFileSync(this.filePath, 'utf8');
115-
this.data = JSON.parse(fileContent);
114+
try {
115+
const fileContent = fs.readFileSync(this.filePath, 'utf8');
116+
this.data = JSON.parse(fileContent);
117+
}
118+
catch (e) {
119+
this.data = [];
120+
}
116121
}
117122

118123
public stopAutoSave(): void {

src/statistics/NumberStats.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ export class NumberStorage {
6666
}
6767

6868
private loadFromFile(): void {
69-
const data = fs.readFileSync(this.filePath, 'utf8');
70-
this.data = JSON.parse(data);
69+
try {
70+
const data = fs.readFileSync(this.filePath, 'utf8');
71+
this.data = JSON.parse(data);
72+
}
73+
catch (e) {
74+
this.data = [];
75+
}
7176
}
7277

7378
public stopAutoSave(): void {

0 commit comments

Comments
 (0)