Skip to content

Commit 1574c58

Browse files
committed
wip
1 parent 366de9f commit 1574c58

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/node/app.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ export const createApp = async (args: DefaultedArgs): Promise<[Express, Express,
3333
resolve2()
3434
}
3535
server.on("error", (err) => {
36-
if (!resolved) {
37-
reject(err)
38-
} else {
39-
// Promise resolved earlier so this is an unrelated error.
40-
util.logError(logger, "http server error", err)
41-
}
36+
handleServerError(resolved, err, reject)
4237
})
4338

4439
if (args.socket) {
@@ -76,3 +71,26 @@ export const ensureAddress = (server: http.Server): string => {
7671
}
7772
return addr
7873
}
74+
75+
/**
76+
* Handles error events from the server.
77+
*
78+
* If the outlying Promise didn't resolve
79+
* then we reject with the error.
80+
*
81+
* Otherwise, we log the error.
82+
*
83+
* We extracted into a function so that we could
84+
* test this logic more easily.
85+
*/
86+
export const handleServerError = (resolved: boolean, err: Error, reject: (err: Error) => void) => {
87+
// Promise didn't resolve earlier so this means it's an error
88+
// that occurs before the server can successfully listen.
89+
// Possibly triggered by listening on an invalid port or socket.
90+
if (!resolved) {
91+
reject(err)
92+
} else {
93+
// Promise resolved earlier so this is an unrelated error.
94+
util.logError(logger, "http server error", err)
95+
}
96+
}

test/unit/node/app.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,6 @@ describe("ensureAddress", () => {
121121
expect(address).toBe(`http://localhost:8080`)
122122
})
123123
})
124+
125+
// TODO@jsjoeio
126+
// write tests for handleServerError

0 commit comments

Comments
 (0)