Skip to content

Commit f6fc974

Browse files
authored
Update message on package not found error (#58740)
1 parent 7c911b9 commit f6fc974

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
},
113113
"nodemonConfig": {
114114
"ext": "ts,json,yml,md,html,scss",
115-
"exec": "tsx",
115+
"exec": "tsx --import ./src/observability/lib/handle-package-not-found.ts",
116116
"ignore": [
117117
"assets",
118118
"script",

src/observability/lib/handle-exceptions.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import FailBot from './failbot'
22

3-
process.on('uncaughtException', async (err: Error) => {
4-
if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {
5-
console.error('\n\n🔥 Uh oh! It looks you are missing a required npm module.')
6-
console.error(
7-
'Please run `npm install` to make sure you have all the required dependencies.\n\n',
8-
)
9-
}
10-
3+
process.on('uncaughtException', async (err: Error | unknown) => {
114
console.error(err)
125
try {
13-
FailBot.report(err)
6+
// Type guard to ensure we have an Error object for FailBot
7+
const error = err instanceof Error ? err : new Error(JSON.stringify(err))
8+
FailBot.report(error)
149
} catch (failBotError) {
1510
console.warn('Even sending the uncaughtException error to FailBot failed!')
1611
console.error(failBotError)
@@ -21,7 +16,7 @@ process.on('unhandledRejection', async (err: Error | unknown) => {
2116
console.error(err)
2217
try {
2318
// Type guard to ensure we have an Error object for FailBot
24-
const error = err instanceof Error ? err : new Error(String(err))
19+
const error = err instanceof Error ? err : new Error(JSON.stringify(err))
2520
FailBot.report(error)
2621
} catch (failBotError) {
2722
console.warn('Even sending the unhandledRejection error to FailBot failed!')
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
This file adds a custom error message if a package is missing
3+
to prompt the contributor to run `npm ci`.
4+
This handler must be separate from handle-exceptions.ts in order to function.
5+
It's imported in package.json in nodemonConfig,
6+
whereas that is imported in start-server.ts.
7+
This file should not import any packages.
8+
We are suggesting `npm ci` to contributors
9+
to avoid unexpected changes to the package-lock.json file.
10+
All other errors should fall through to the error handler in handle-exceptions.ts.
11+
*/
12+
13+
type ErrorWithCode = {
14+
code: string
15+
message: string
16+
}
17+
18+
const ERROR_TYPES = [
19+
'MODULE_NOT_FOUND',
20+
'ERR_MODULE_NOT_FOUND',
21+
'ERR_PACKAGE_PATH_NOT_EXPORTED',
22+
'ERR_PACKAGE_IMPORT_NOT_DEFINED',
23+
]
24+
25+
function isErrorWithCode(err: unknown): err is ErrorWithCode {
26+
return (
27+
typeof err === 'object' &&
28+
err !== null &&
29+
'code' in err &&
30+
'message' in err &&
31+
typeof err.code === 'string' &&
32+
typeof err.message === 'string'
33+
)
34+
}
35+
36+
process.on('uncaughtException', async (err: Error | unknown) => {
37+
const isMissingModuleError = isErrorWithCode(err) && ERROR_TYPES.includes(err.code)
38+
39+
if (isMissingModuleError) {
40+
console.error(`${err.code}:`, err.message)
41+
console.error('\n==========================================')
42+
console.error('Some dependencies are missing. Please run:')
43+
console.error(' npm ci')
44+
console.error('==========================================\n')
45+
process.exit(1)
46+
}
47+
})

0 commit comments

Comments
 (0)