mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 16:25:21 +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 }), {});
|
||||
|
||||
return modelRecords;
|
||||
} catch (_err) {
|
||||
const err = _err as NodeJS.ErrnoException;
|
||||
} catch (err) {
|
||||
if (!isErrorErrNoException(err) || !isMissingFileError(err)) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (err?.code === "ENOENT") {
|
||||
if (err.syscall === "open") {
|
||||
// file opening failed
|
||||
if (isFileReadingFailure(err)) {
|
||||
throw new Error(
|
||||
Messages.format(
|
||||
Messages.MODELS.FILE_NO_OPEN,
|
||||
JSON.stringify(err),
|
||||
),
|
||||
);
|
||||
} else if (err.syscall === "scandir") {
|
||||
// directory reading failed
|
||||
}
|
||||
|
||||
if (isDirectoryReadingFailure(err)) {
|
||||
throw new Error(
|
||||
Messages.format(Messages.MODELS.DIR_NOT_FOUND, err.path),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
* @param filePath
|
||||
|
||||
Reference in New Issue
Block a user