Skip to content

Commit 3032be4

Browse files
committed
refactor: collect error message strings in text.ts
1 parent cee16e3 commit 3032be4

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 {
@@ -199,31 +200,31 @@ export const importFileConvertAndLoadListener =
199200

200201
try {
201202
if (!resourceFilePath.trim() || !fs.existsSync(resourceFilePath)) {
202-
throw new Error('Input file does not exist');
203+
throw new Error(text.backendError.inputFileDoesNotExist);
203204
}
204205

205206
try {
206207
fs.accessSync(resourceFilePath, fs.constants.R_OK);
207208
} catch (error) {
208-
throw new Error('Permission error: cannot read input file');
209+
throw new Error(text.backendError.inputFilePermissionError);
209210
}
210211

211212
if (!opossumFilePath.trim()) {
212-
throw new Error('No .opossum save location selected');
213+
throw new Error(text.backendError.opossumFileNotSelected);
213214
}
214215

215216
if (!opossumFilePath.endsWith('.opossum')) {
216-
throw new Error('Output file name must have .opossum extension');
217+
throw new Error(text.backendError.opossumFileWrongExtension);
217218
}
218219

219220
if (!fs.existsSync(path.dirname(opossumFilePath))) {
220-
throw new Error('Output directory does not exist');
221+
throw new Error(text.backendError.opossumFileDirectoryDoesNotExist);
221222
}
222223

223224
try {
224225
fs.accessSync(path.dirname(opossumFilePath), fs.constants.W_OK);
225226
} catch (error) {
226-
throw new Error('Permission error: cannot write to output directory');
227+
throw new Error(text.backendError.opossumFilePermissionError);
227228
}
228229

229230
logger.info('Converting input file to .opossum format');
@@ -259,19 +260,19 @@ export function getMergeFileAndLoadListener(
259260
fileType: FileType,
260261
) => {
261262
if (!inputFilePath.trim() || !fs.existsSync(inputFilePath)) {
262-
throw new Error('Input file does not exist');
263+
throw new Error(text.backendError.inputFileDoesNotExist);
263264
}
264265

265266
try {
266267
fs.accessSync(inputFilePath, fs.constants.R_OK);
267268
} catch (error) {
268-
throw new Error('Permission error: cannot read input file');
269+
throw new Error(text.backendError.inputFilePermissionError);
269270
}
270271

271272
const currentOpossumFilePath = getGlobalBackendState().opossumFilePath;
272273

273274
if (!currentOpossumFilePath) {
274-
throw new Error('No open file to merge into');
275+
throw new Error(text.backendError.noOpenFileToMergeInto);
275276
}
276277

277278
try {
@@ -280,9 +281,7 @@ export function getMergeFileAndLoadListener(
280281
`${currentOpossumFilePath}.backup`,
281282
);
282283
} catch (error) {
283-
throw new Error(
284-
'Unable to create backup of currently open Opossum file',
285-
);
284+
throw new Error(text.backendError.cantCreateBackup);
286285
}
287286

288287
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)