Skip to content

Commit 1b56a20

Browse files
committed
refactor: improve error messages
1 parent 32bd39c commit 1b56a20

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/index.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function getAsyncAwareProcessPathCallback(isLoadCallbackAsync: boolean, loadCall
173173
if (isLoadCallbackAsync) {
174174
async function processPathAsync(folderPath: string) {
175175
if (typeof folderPath !== "string" || folderPath.trim() === "") {
176-
throw new Error(`Invalid folder path: ${folderPath}. Must be a non-empty string.`);
176+
throw new Error(`Invalid folder path: '${folderPath}'. Must be a non-empty string.`);
177177
}
178178
const folderName = nodePath.basename(folderPath);
179179
await loadCallback(folderPath, folderName);
@@ -182,7 +182,7 @@ function getAsyncAwareProcessPathCallback(isLoadCallbackAsync: boolean, loadCall
182182
}
183183
function processPathSync(folderPath: string) {
184184
if (typeof folderPath !== "string" || folderPath.trim() === "") {
185-
throw new Error(`Invalid folder path: ${folderPath}. Must be a non-empty string.`);
185+
throw new Error(`Invalid folder path: '${folderPath}'. Must be a non-empty string.`);
186186
}
187187
const folderName = nodePath.basename(folderPath);
188188
loadCallback(folderPath, folderName);
@@ -244,32 +244,32 @@ async function processPaths(paths: string[], processMode: string, processPathCal
244244

245245
async function loadFolders(folderPaths: string[], loadCallback: LoadFoldersCallback, options?: LoadFoldersOptions) {
246246
if (!Array.isArray(folderPaths)) {
247-
throw new Error(`Invalid paths: ${folderPaths}. Must be an array.`);
247+
throw new Error(`Invalid paths: '${folderPaths}'. Must be an array.`);
248248
}
249249
if (typeof loadCallback !== "function") {
250-
throw new Error(`Invalid load callback: ${loadCallback}. Must be a function.`);
250+
throw new Error(`Invalid load callback: '${loadCallback}'. Must be a function.`);
251251
}
252252
if (options !== undefined && (options === null || typeof options !== "object" || Array.isArray(options))) {
253253
throw new Error(`Invalid options: '${options}'. Must be a an object.`);
254254
}
255255
const loadOptions = { ...DEFAULT_LOAD_FOLDER_OPTIONS, ...(options || {}) };
256256
const processMode = loadOptions.processMode;
257257
if (!DEFAULT_PROCESS_MODES.includes(processMode)) {
258-
throw new Error(`Invalid process mode: ${processMode}. Must be one of string: ${DEFAULT_PROCESS_MODES.join(", ")}`);
258+
throw new Error(`Invalid process mode: '${processMode}'. Must be one of string: ${DEFAULT_PROCESS_MODES.join(", ")}`);
259259
}
260260
const isLoadCallbackAsync = nodeUtilTypes.isAsyncFunction(loadCallback);
261261
if (processMode === "concurrent" && !isLoadCallbackAsync) {
262-
throw new Error("Invalid load callback. Process mode: concurrent requires an asynchronous load callback.");
262+
throw new Error("Invalid load callback. Process mode: 'concurrent' requires an asynchronous load callback.");
263263
}
264264
return await processPaths(folderPaths, processMode, getAsyncAwareProcessPathCallback(isLoadCallbackAsync, loadCallback));
265265
}
266266

267267
async function loadModules(modulePaths: string[], loadCallback: LoadModulesCallback, options?: LoadModulesOptions) {
268268
if (!Array.isArray(modulePaths)) {
269-
throw new Error(`Invalid paths: ${modulePaths}. Must be an array.`);
269+
throw new Error(`Invalid paths: '${modulePaths}'. Must be an array.`);
270270
}
271271
if (typeof loadCallback !== "function") {
272-
throw new Error(`Invalid load callback: ${loadCallback}. Must be a function.`);
272+
throw new Error(`Invalid load callback: '${loadCallback}'. Must be a function.`);
273273
}
274274
if (options !== undefined && (options === null || typeof options !== "object" || Array.isArray(options))) {
275275
throw new Error(`Invalid options: '${options}'. Must be a an object.`);
@@ -280,24 +280,24 @@ async function loadModules(modulePaths: string[], loadCallback: LoadModulesCallb
280280
const preferredExportName = loadOptions.preferredExportName;
281281
const isImportEnabled = loadOptions.isImportEnabled;
282282
if (!DEFAULT_PROCESS_MODES.includes(processMode)) {
283-
throw new Error(`Invalid process mode: ${processMode}. Must be one of string: ${DEFAULT_PROCESS_MODES.join(", ")}`);
283+
throw new Error(`Invalid process mode: '${processMode}'. Must be one of string: ${DEFAULT_PROCESS_MODES.join(", ")}`);
284284
}
285285
if (!DEFAULT_EXPORT_TYPES.includes(exportType)) {
286-
throw new Error(`Invalid exportType: ${exportType}. Must be one of string: ${DEFAULT_EXPORT_TYPES.join(", ")}`);
286+
throw new Error(`Invalid exportType: '${exportType}'. Must be one of string: ${DEFAULT_EXPORT_TYPES.join(", ")}`);
287287
}
288288
if (typeof preferredExportName !== "string" || preferredExportName.trim() === "") {
289-
throw new Error(`Invalid preferred export name: ${preferredExportName}. Must be a non-empty string.`);
289+
throw new Error(`Invalid preferred export name: '${preferredExportName}'. Must be a non-empty string.`);
290290
}
291291
if (typeof isImportEnabled !== "boolean") {
292-
throw new Error(`Invalid isImportEnabled: ${isImportEnabled}. Must be a boolean.`);
292+
throw new Error(`Invalid isImportEnabled: '${isImportEnabled}'. Must be a boolean.`);
293293
}
294294
const isLoadCallbackAsync = nodeUtilTypes.isAsyncFunction(loadCallback);
295295
if (processMode === "concurrent" && !isLoadCallbackAsync) {
296-
throw new Error("Invalid load callback. Process mode: concurrent requires an asynchronous load callback.");
296+
throw new Error("Invalid load callback. Process mode: 'concurrent' requires an asynchronous load callback.");
297297
}
298298
async function processPath(filePath: string) {
299299
if (typeof filePath !== "string" || filePath.trim() === "") {
300-
throw new Error(`Invalid module path: ${filePath}. Must be a non-empty string.`);
300+
throw new Error(`Invalid module path: '${filePath}'. Must be a non-empty string.`);
301301
}
302302
const fileUrlHref = nodeUrl.pathToFileURL(filePath).href;
303303
const fileName = nodePath.basename(fileUrlHref);

0 commit comments

Comments
 (0)