Skip to content

Commit eed69ed

Browse files
committed
fix(fs): add comprehensive error handling for readJson operations
Improve error messages for JSON file reading failures in both async and sync versions: - ENOENT: File not found with guidance on creating the file - EACCES/EPERM: Permission denied with access guidance Users now get actionable guidance instead of generic ENOENT/EACCES errors. Also fixes formatting in dlx-package.ts for long condition.
1 parent 62aa6d1 commit eed69ed

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/dlx-package.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ async function ensurePackageInstalled(
200200
{ cause: e },
201201
)
202202
}
203-
if (code === 'ENOTFOUND' || code === 'ETIMEDOUT' || code === 'EAI_AGAIN') {
203+
if (
204+
code === 'ENOTFOUND' ||
205+
code === 'ETIMEDOUT' ||
206+
code === 'EAI_AGAIN'
207+
) {
204208
throw new Error(
205209
`Network error installing ${packageSpec}\n` +
206210
'Check your internet connection and try again.',

src/fs.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,21 @@ export async function readJson(
984984
})
985985
} catch (e) {
986986
if (shouldThrow) {
987+
const code = (e as NodeJS.ErrnoException).code
988+
if (code === 'ENOENT') {
989+
throw new Error(
990+
`JSON file not found: ${filepath}\n` +
991+
'Ensure the file exists or create it with the expected structure.',
992+
{ cause: e },
993+
)
994+
}
995+
if (code === 'EACCES' || code === 'EPERM') {
996+
throw new Error(
997+
`Permission denied reading JSON file: ${filepath}\n` +
998+
'Check file permissions or run with appropriate access.',
999+
{ cause: e },
1000+
)
1001+
}
9871002
throw e
9881003
}
9891004
return undefined
@@ -1046,6 +1061,21 @@ export function readJsonSync(
10461061
})
10471062
} catch (e) {
10481063
if (shouldThrow) {
1064+
const code = (e as NodeJS.ErrnoException).code
1065+
if (code === 'ENOENT') {
1066+
throw new Error(
1067+
`JSON file not found: ${filepath}\n` +
1068+
'Ensure the file exists or create it with the expected structure.',
1069+
{ cause: e },
1070+
)
1071+
}
1072+
if (code === 'EACCES' || code === 'EPERM') {
1073+
throw new Error(
1074+
`Permission denied reading JSON file: ${filepath}\n` +
1075+
'Check file permissions or run with appropriate access.',
1076+
{ cause: e },
1077+
)
1078+
}
10491079
throw e
10501080
}
10511081
return undefined

0 commit comments

Comments
 (0)