Skip to content

Log errors, make it easier to debug errors inside @elysiajs/static #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ const htmlCache = new Cache({
})

const listFiles = async (dir: string): Promise<string[]> => {
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)]
})
Expand Down Expand Up @@ -310,10 +311,14 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
if (shouldIgnore(path)) throw new NotFoundError()

try {
let status = statCache.get<Stats>(path)
if (!status) {
status = await stat(path)
statCache.set(path, status)
let fileStat = statCache.get<Stats>(path)
if (!fileStat) {
try {
fileStat = await stat(path)
statCache.set(path, fileStat)
} catch {
throw new NotFoundError()
}
}

let file =
Expand All @@ -322,7 +327,7 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
)

if (!file) {
if (status.isDirectory()) {
if (fileStat.isDirectory()) {
let hasCache = false

if (
Expand Down Expand Up @@ -378,6 +383,8 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
headers
})
} catch (error) {
if (error instanceof NotFoundError) throw error
console.error(`Error in @elysiajs/static`, error)
throw new NotFoundError()
}
}
Expand Down