Skip to content

Commit 7b20da3

Browse files
committed
Remove unnecessary try catch in CalculationsRepository since it already exists in StorageRepository, catch in usePopup instead
1 parent c6fe106 commit 7b20da3

File tree

2 files changed

+46
-64
lines changed

2 files changed

+46
-64
lines changed

src/data/calculations/CalculationsRepository.ts

+33-57
Original file line numberDiff line numberDiff line change
@@ -8,83 +8,59 @@ export class CalculationsRepository implements ICalculationsRepository {
88
remoteDataSource: IStorageRepository = IStorageRepository.instance;
99

1010
async storeCalculation(calculationData: CalculationData): Promise<void> {
11-
try {
12-
const oldCalculations = await this.getAllCalculations();
13-
const newCalculations = [calculationData, ...oldCalculations];
14-
await this.remoteDataSource.set({
15-
allCalculations: JSON.stringify(newCalculations),
16-
});
17-
} catch (e: unknown) {
18-
throw Error(e as string);
19-
}
11+
const oldCalculations = await this.getAllCalculations();
12+
const newCalculations = [calculationData, ...oldCalculations];
13+
await this.remoteDataSource.set({
14+
allCalculations: JSON.stringify(newCalculations),
15+
});
2016
}
2117

2218
async cacheOngoingCalculation(
2319
calculationData: CalculationData
2420
): Promise<void> {
25-
try {
26-
await this.remoteDataSource.set({
27-
ongoingCalculation: JSON.stringify(calculationData),
28-
});
29-
} catch (e: unknown) {
30-
throw Error(e as string);
31-
}
21+
await this.remoteDataSource.set({
22+
ongoingCalculation: JSON.stringify(calculationData),
23+
});
3224
}
3325

3426
async clearOngoingCalculation(): Promise<void> {
35-
try {
36-
await this.remoteDataSource.set({
37-
ongoingCalculation: null,
38-
});
39-
} catch (e: unknown) {
40-
throw Error(e as string);
41-
}
27+
await this.remoteDataSource.set({
28+
ongoingCalculation: null,
29+
});
4230
}
4331

4432
async getAllCalculations(): Promise<CalculationData[]> {
45-
try {
46-
const data = await this.remoteDataSource.get({
47-
allCalculations: JSON.stringify([]),
48-
});
33+
const data = await this.remoteDataSource.get({
34+
allCalculations: JSON.stringify([]),
35+
});
4936

50-
return JSON.parse(
51-
data["allCalculations"] as string
52-
) as CalculationData[];
53-
} catch (e: unknown) {
54-
throw Error(e as string);
55-
}
37+
return JSON.parse(
38+
data["allCalculations"] as string
39+
) as CalculationData[];
5640
}
5741

5842
async _getOngoingCalculation(): Promise<CalculationData | null> {
59-
try {
60-
const data = await this.remoteDataSource.get({
61-
ongoingCalculation: null,
62-
});
43+
const data = await this.remoteDataSource.get({
44+
ongoingCalculation: null,
45+
});
6346

64-
if (data["ongoingCalculation"] !== null) {
65-
return JSON.parse(
66-
data["ongoingCalculation"] as string
67-
) as CalculationData;
68-
}
69-
return null;
70-
} catch (e: unknown) {
71-
throw Error(e as string);
47+
if (data["ongoingCalculation"] !== null) {
48+
return JSON.parse(
49+
data["ongoingCalculation"] as string
50+
) as CalculationData;
7251
}
52+
return null;
7353
}
7454

7555
async getLastCalculation(): Promise<CalculationData | null> {
76-
try {
77-
const ongoingCalculation = await this._getOngoingCalculation();
78-
if (ongoingCalculation !== null) {
79-
return ongoingCalculation;
80-
}
81-
const oldCalculations = await this.getAllCalculations();
82-
if (oldCalculations.length > 0) {
83-
return oldCalculations[0];
84-
}
85-
return null;
86-
} catch (e: unknown) {
87-
throw Error(e as string);
56+
const ongoingCalculation = await this._getOngoingCalculation();
57+
if (ongoingCalculation !== null) {
58+
return ongoingCalculation;
59+
}
60+
const oldCalculations = await this.getAllCalculations();
61+
if (oldCalculations.length > 0) {
62+
return oldCalculations[0];
8863
}
64+
return null;
8965
}
9066
}

src/usePopup.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,19 @@ export const usePopup = () => {
125125
}
126126
}
127127
});
128-
calculationsRepository.storeCalculation({
129-
bytes: totalBytesTransferred,
130-
emissions: emissions,
131-
specificEmissions: averageSpecificEmissions,
132-
selectedCountries: selectedCountries,
133-
});
134-
calculationsRepository.clearOngoingCalculation();
128+
try {
129+
calculationsRepository.storeCalculation({
130+
bytes: totalBytesTransferred,
131+
emissions: emissions,
132+
specificEmissions: averageSpecificEmissions,
133+
selectedCountries: selectedCountries,
134+
});
135+
calculationsRepository.clearOngoingCalculation();
136+
} catch (e: unknown) {
137+
if (e instanceof Error) {
138+
setError(e.message);
139+
}
140+
}
135141
};
136142

137143
const addSelectedCountry = async (country: CountryName) => {

0 commit comments

Comments
 (0)