mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 18:25:24 +00:00
Improved getModelFolderContents error handling readability
This commit is contained in:
@@ -50,30 +50,52 @@ export default async function getModelFolderContents(
|
|||||||
.reduce((acc, current) => ({ ...acc, ...current }), {});
|
.reduce((acc, current) => ({ ...acc, ...current }), {});
|
||||||
|
|
||||||
return modelRecords;
|
return modelRecords;
|
||||||
} catch (_err) {
|
} catch (err) {
|
||||||
const err = _err as NodeJS.ErrnoException;
|
if (!isErrorErrNoException(err) || !isMissingFileError(err)) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
if (err?.code === "ENOENT") {
|
if (isFileReadingFailure(err)) {
|
||||||
if (err.syscall === "open") {
|
throw new Error(
|
||||||
// file opening failed
|
Messages.format(
|
||||||
throw new Error(
|
Messages.MODELS.FILE_NO_OPEN,
|
||||||
Messages.format(
|
JSON.stringify(err),
|
||||||
Messages.MODELS.FILE_NO_OPEN,
|
),
|
||||||
JSON.stringify(err),
|
);
|
||||||
),
|
}
|
||||||
);
|
|
||||||
} else if (err.syscall === "scandir") {
|
if (isDirectoryReadingFailure(err)) {
|
||||||
// directory reading failed
|
throw new Error(
|
||||||
throw new Error(
|
Messages.format(Messages.MODELS.DIR_NOT_FOUND, err.path),
|
||||||
Messages.format(Messages.MODELS.DIR_NOT_FOUND, err.path),
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isErrorErrNoException(err: unknown): err is NodeJS.ErrnoException {
|
||||||
|
return Object.prototype.hasOwnProperty.call(err, "errno");
|
||||||
|
}
|
||||||
|
|
||||||
|
function isMissingFileError(
|
||||||
|
err: unknown,
|
||||||
|
): err is NodeJS.ErrnoException & { code: "ENOENT" } {
|
||||||
|
return (err as NodeJS.ErrnoException).code === "ENOENT";
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDirectoryReadingFailure(
|
||||||
|
err: NodeJS.ErrnoException,
|
||||||
|
): err is NodeJS.ErrnoException & { syscall: "scandir" } {
|
||||||
|
return err.syscall === "scandir";
|
||||||
|
}
|
||||||
|
|
||||||
|
function isFileReadingFailure(
|
||||||
|
err: NodeJS.ErrnoException,
|
||||||
|
): err is NodeJS.ErrnoException & { syscall: "open" } {
|
||||||
|
return err.syscall === "open";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads sequentially
|
* Reads sequentially
|
||||||
* @param filePath
|
* @param filePath
|
||||||
|
|||||||
Reference in New Issue
Block a user