Skip to content

Commit d5ed96c

Browse files
committed
refactor: collect error message strings in text.ts
1 parent 879e724 commit d5ed96c

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

src/ElectronBackend/main/listeners.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
PackageInfo,
2525
SaveFileArgs,
2626
} from '../../shared/shared-types';
27+
import { text } from '../../shared/text';
2728
import { writeFile, writeOpossumFile } from '../../shared/write-file';
2829
import { LoadedFileFormat } from '../enums/enums';
2930
import {
@@ -206,31 +207,31 @@ export function getImportFileConvertAndLoadListener(
206207
opossumFilePath: string,
207208
) => {
208209
if (!resourceFilePath.trim() || !fs.existsSync(resourceFilePath)) {
209-
throw new Error('Input file does not exist');
210+
throw new Error(text.backendError.inputFileDoesNotExist);
210211
}
211212

212213
try {
213214
fs.accessSync(resourceFilePath, fs.constants.R_OK);
214215
} catch (error) {
215-
throw new Error('Permission error: cannot read input file');
216+
throw new Error(text.backendError.inputFilePermissionError);
216217
}
217218

218219
if (!opossumFilePath.trim()) {
219-
throw new Error('No .opossum save location selected');
220+
throw new Error(text.backendError.opossumFileNotSelected);
220221
}
221222

222223
if (!opossumFilePath.endsWith('.opossum')) {
223-
throw new Error('Output file name must have .opossum extension');
224+
throw new Error(text.backendError.opossumFileWrongExtension);
224225
}
225226

226227
if (!fs.existsSync(path.dirname(opossumFilePath))) {
227-
throw new Error('Output directory does not exist');
228+
throw new Error(text.backendError.opossumFileDirectoryDoesNotExist);
228229
}
229230

230231
try {
231232
fs.accessSync(path.dirname(opossumFilePath), fs.constants.W_OK);
232233
} catch (error) {
233-
throw new Error('Permission error: cannot write to output directory');
234+
throw new Error(text.backendError.opossumFilePermissionError);
234235
}
235236

236237
logger.info('Converting input file to .opossum format');
@@ -263,19 +264,19 @@ export function getMergeFileAndLoadListener(
263264
fileType: FileType,
264265
) => {
265266
if (!inputFilePath.trim() || !fs.existsSync(inputFilePath)) {
266-
throw new Error('Input file does not exist');
267+
throw new Error(text.backendError.inputFileDoesNotExist);
267268
}
268269

269270
try {
270271
fs.accessSync(inputFilePath, fs.constants.R_OK);
271272
} catch (error) {
272-
throw new Error('Permission error: cannot read input file');
273+
throw new Error(text.backendError.inputFilePermissionError);
273274
}
274275

275276
const currentOpossumFilePath = getGlobalBackendState().opossumFilePath;
276277

277278
if (!currentOpossumFilePath) {
278-
throw new Error('No open file to merge into');
279+
throw new Error(text.backendError.noOpenFileToMergeInto);
279280
}
280281

281282
try {
@@ -284,9 +285,7 @@ export function getMergeFileAndLoadListener(
284285
`${currentOpossumFilePath}.backup`,
285286
);
286287
} catch (error) {
287-
throw new Error(
288-
'Unable to create backup of currently open Opossum file',
289-
);
288+
throw new Error(text.backendError.cantCreateBackup);
290289
}
291290

292291
logger.info('Merging input file into current .opossum file');

src/ElectronBackend/opossum-file/ExternalFileConverter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
33
//
44
// SPDX-License-Identifier: Apache-2.0
5+
import { text } from '../../shared/text';
56
import { FileConverter } from './FileConverter';
67

78
export abstract class ExternalFileConverter extends FileConverter {
@@ -22,7 +23,7 @@ export abstract class ExternalFileConverter extends FileConverter {
2223
toBeConvertedFilePath,
2324
]);
2425
} catch (error) {
25-
throw new Error(`Input file is not a valid ${this.fileTypeName} file`);
26+
throw new Error(text.backendError.inputFileInvalid(this.fileTypeName));
2627
}
2728
}
2829
}

src/ElectronBackend/opossum-file/FileConverter.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import fs from 'fs';
88
import { join } from 'path';
99
import { promisify } from 'util';
1010

11+
import { text } from '../../shared/text';
12+
1113
export abstract class FileConverter {
1214
protected abstract readonly fileTypeSwitch: string;
1315
protected abstract readonly fileTypeName: string;
@@ -48,7 +50,7 @@ export abstract class FileConverter {
4850
opossumFilePath,
4951
]);
5052
} catch (error) {
51-
throw new Error(`Input file is not a valid ${this.fileTypeName} file`);
53+
throw new Error(text.backendError.inputFileInvalid(this.fileTypeName));
5254
}
5355

5456
if (preConvertedFilePath) {

src/ElectronBackend/opossum-file/LegacyFileConverter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import path from 'path';
88
import zlib from 'zlib';
99

1010
import { legacyOutputFileEnding } from '../../Frontend/shared-constants';
11+
import { text } from '../../shared/text';
1112
import { writeOpossumFile } from '../../shared/write-file';
1213
import { getFilePathWithAppendix } from '../utils/getFilePathWithAppendix';
1314
import { FileConverter } from './FileConverter';
@@ -82,7 +83,7 @@ export class LegacyFileConverter extends FileConverter {
8283
output: this.readOutputJson(pathToInputJson),
8384
});
8485
} catch (error) {
85-
throw new Error('Input file is not a valid Legacy Opossum file');
86+
throw new Error(text.backendError.inputFileInvalid(this.fileTypeName));
8687
}
8788
}
8889

src/shared/text.ts

+13
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,17 @@ export const text = {
336336
: `Select file to merge (${fileFormat.extensions.map((ext) => `.${ext}`).join('/')})`,
337337
},
338338
},
339+
backendError: {
340+
inputFileDoesNotExist: 'Input file does not exist',
341+
inputFilePermissionError: 'Permission error: cannot read input file',
342+
inputFileInvalid: (fileTypeName: string) =>
343+
`Input file is not a valid ${fileTypeName} file`,
344+
opossumFileNotSelected: 'No .opossum save location selected',
345+
opossumFileWrongExtension: 'Output file name must have .opossum extension',
346+
opossumFileDirectoryDoesNotExist: 'Output directory does not exist',
347+
opossumFilePermissionError:
348+
'Permission error: cannot write to output directory',
349+
noOpenFileToMergeInto: 'No open file to merge into',
350+
cantCreateBackup: 'Unable to create backup of currently open Opossum file',
351+
},
339352
} as const;

0 commit comments

Comments
 (0)