Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

fix: add more error info on missing functions folders #1134

Merged
merged 3 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,36 @@ export const safeUnlink = async (path: string) => {
// filenames within those directories, if at least one of the directories
// exists. If not, an error is thrown.
export const listFunctionsDirectories = async function (srcFolders: string[]) {
const filenamesByDirectory = await Promise.all(
srcFolders.map(async (srcFolder) => {
try {
const filenames = await listFunctionsDirectory(srcFolder)
const filenamesByDirectory = await Promise.allSettled(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allSettled is available as of node 12.9.0

srcFolders.map((srcFolder) => listFunctionsDirectory(srcFolder)),
)
const errorMessages: string[] = []
const validDirectories = filenamesByDirectory
.map((result) => {
if (result.status === 'rejected') {
if (result.reason instanceof Error && (result.reason as NodeJS.ErrnoException).code !== 'ENOENT') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason Error does not contain code, so this is the workaround.

danez marked this conversation as resolved.
Show resolved Hide resolved
throw result.reason
}

return filenames
} catch {
return null
}
}),
)
const validDirectories = filenamesByDirectory.filter(nonNullable)

return result.value
})
.filter(nonNullable)

if (validDirectories.length === 0) {
throw new Error(`Functions folder does not exist: ${srcFolders.join(', ')}`)
throw new Error(`Functions folders do not exist: ${srcFolders.join(', ')}
${errorMessages.join('\n')}`)
danez marked this conversation as resolved.
Show resolved Hide resolved
}

return validDirectories.flat()
}

export const listFunctionsDirectory = async function (srcFolder: string) {
try {
const filenames = await fs.readdir(srcFolder)
const listFunctionsDirectory = async function (srcFolder: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the export as it is nowhere used outside this file, and also is not a Public API.

const filenames = await fs.readdir(srcFolder)

return filenames.map((name) => join(srcFolder, name))
} catch {
throw new Error(`Functions folder does not exist: ${srcFolder}`)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we do not export it we do not need to catch anymore, because the error will be matched in the function above.

return filenames.map((name) => join(srcFolder, name))
}

export const resolveFunctionsDirectories = (input: string | string[]) => {
Expand Down
Empty file.
18 changes: 17 additions & 1 deletion tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ testMany(
['bundler_default', 'bundler_esbuild', 'bundler_esbuild_zisi', 'bundler_default_nft', 'bundler_nft'],
async (options, t) => {
await t.throwsAsync(zipNode(t, 'does-not-exist', { opts: options }), {
message: /Functions folder does not exist/,
message: /Functions folders do not exist/,
})
},
)
Expand Down Expand Up @@ -2700,6 +2700,22 @@ test('listFunction includes in-source config declarations', async (t) => {
})
})

test('listFunctionsFiles throws if all function directories do not exist', async (t) => {
const error = await t.throwsAsync(
async () =>
await listFunctionsFiles([
join(FIXTURES_DIR, 'missing-functions-folder', 'functions'),
join(FIXTURES_DIR, 'missing-functions-folder', 'functions2'),
]),
)
t.regex(error.message, /Functions folders do not exist: /)
})

test('listFunctionsFiles does not hide errors that have nothing todo with folder existents', async (t) => {
const error = await t.throwsAsync(async () => await listFunctionsFiles([true]))
t.notRegex(error.message, /Functions folders do not exist: /)
})

test('listFunctionsFiles includes in-source config declarations', async (t) => {
const functions = await listFunctionsFiles(join(FIXTURES_DIR, 'in-source-config', 'functions'), {
parseISC: true,
Expand Down