Skip to content

Commit

Permalink
better loading error handling + use full file paths with extension fo…
Browse files Browse the repository at this point in the history
…r loading files
  • Loading branch information
tada5hi committed Aug 11, 2022
1 parent 67a7050 commit 3978b1c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
7 changes: 3 additions & 4 deletions src/loader/file-type/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
*/

import fs from 'fs';
import { handleFileLoadError } from '../../utils';

export async function loadJsonFile(filePath: string) : Promise<unknown | undefined> {
try {
const file = await fs.promises.readFile(filePath);
return JSON.parse(file.toString('utf-8'));
} catch (e) {
/* istanbul ignore next */
return undefined;
return handleFileLoadError(e);
}
}

Expand All @@ -22,7 +22,6 @@ export function loadJsonFileSync(filePath: string) : unknown | undefined {
const file = fs.readFileSync(filePath);
return JSON.parse(file.toString('utf-8'));
} catch (e) {
/* istanbul ignore next */
return undefined;
return handleFileLoadError(e);
}
}
7 changes: 3 additions & 4 deletions src/loader/file-type/script/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getExportItem } from './utils';
import { LoaderFilterFn, ScriptFileExportItem } from './type';
import { LocatorInfo, isLocatorInfo } from '../../../locator';
import { buildLoaderFilePath } from '../../utils';
import { handleFileLoadError } from '../../../utils';

export async function loadScriptFile(data: LocatorInfo | string) : Promise<unknown | undefined> {
const filePath = isLocatorInfo(data) ?
Expand All @@ -18,8 +19,7 @@ export async function loadScriptFile(data: LocatorInfo | string) : Promise<unkno
try {
return await import(filePath);
} catch (e) {
/* istanbul ignore next */
return undefined;
return handleFileLoadError(e);
}
}

Expand All @@ -36,7 +36,6 @@ export async function loadScriptFileExport(

return getExportItem(data, filterFn);
} catch (e) {
/* istanbul ignore next */
return undefined;
return handleFileLoadError(e);
}
}
7 changes: 3 additions & 4 deletions src/loader/file-type/script/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { LoaderFilterFn, ScriptFileExportItem } from './type';
import { getExportItem } from './utils';
import { LocatorInfo, isLocatorInfo } from '../../../locator';
import { buildLoaderFilePath } from '../../utils';
import { handleFileLoadError } from '../../../utils';

export function loadScriptFileSync(data: LocatorInfo | string) : unknown | undefined {
const filePath = isLocatorInfo(data) ?
Expand All @@ -19,8 +20,7 @@ export function loadScriptFileSync(data: LocatorInfo | string) : unknown | undef
// eslint-disable-next-line @typescript-eslint/no-var-requires, global-require,import/no-dynamic-require
return require(filePath);
} catch (e) {
/* istanbul ignore next */
return undefined;
return handleFileLoadError(e);
}
}

Expand All @@ -37,7 +37,6 @@ export function loadScriptFileExportSync(

return getExportItem(data, filterFn);
} catch (e) {
/* istanbul ignore next */
return undefined;
return handleFileLoadError(e);
}
}
3 changes: 1 addition & 2 deletions src/loader/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ import path from 'path';
import { LocatorInfo } from '../locator';

export function buildLoaderFilePath(info: LocatorInfo) {
return path.join(info.path, info.fileName) +
(info.fileExtension === '.json' ? '.json' : '');
return path.join(info.path, info.fileName) + info.fileExtension;
}
19 changes: 19 additions & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2022.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

/* istanbul ignore next */
export function handleFileLoadError(e: unknown) : undefined {
if (e instanceof Error) {
throw e;
}

if (typeof e === 'string') {
throw new Error(e);
}

return undefined;
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
* view the LICENSE file that was distributed with this source code.
*/

export * from './error';
export * from './has-own-property';
export * from './to-array';

0 comments on commit 3978b1c

Please sign in to comment.