diff --git a/src/index.ts b/src/index.ts index 0f12705..e43b0e5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,14 +36,15 @@ const htmlCache = new Cache({ }) const listFiles = async (dir: string): Promise => { - const files = await readdir(dir) + const files = await readdir(dir).catch(() => []) const all = await Promise.all( files.map(async (name) => { const file = dir + sep + name - const stats = await stat(file) + const stats = await stat(file).catch(() => null) + if (!stats) return [] - return stats && stats.isDirectory() + return stats.isDirectory() ? await listFiles(file) : [resolve(dir, file)] }) @@ -310,10 +311,14 @@ export const staticPlugin = async ( if (shouldIgnore(path)) throw new NotFoundError() try { - let status = statCache.get(path) - if (!status) { - status = await stat(path) - statCache.set(path, status) + let fileStat = statCache.get(path) + if (!fileStat) { + try { + fileStat = await stat(path) + statCache.set(path, fileStat) + } catch { + throw new NotFoundError() + } } let file = @@ -322,7 +327,7 @@ export const staticPlugin = async ( ) if (!file) { - if (status.isDirectory()) { + if (fileStat.isDirectory()) { let hasCache = false if ( @@ -378,6 +383,8 @@ export const staticPlugin = async ( headers }) } catch (error) { + if (error instanceof NotFoundError) throw error + console.error(`Error in @elysiajs/static`, error) throw new NotFoundError() } }