Skip to content

Commit f188b63

Browse files
committed
wip: handleArgsSocketCatchError
1 parent c5e7800 commit f188b63

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

src/node/app.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ export const createApp = async (args: DefaultedArgs): Promise<[Express, Express,
4141
try {
4242
await fs.unlink(args.socket)
4343
} catch (error: any) {
44-
// TODO@jsjoeio break this out into a separate error handler
45-
// That way we can handle all cases
46-
if (!isNodeJSErrnoException(error) || error.code !== "ENOENT") {
47-
logger.error(error.message ? error.message : error)
48-
}
44+
handleArgsSocketCatchError(error)
4945
}
5046
server.listen(args.socket, resolve)
5147
} else {
@@ -97,3 +93,16 @@ export const handleServerError = (resolved: boolean, err: Error, reject: (err: E
9793
util.logError(logger, "http server error", err)
9894
}
9995
}
96+
97+
/**
98+
* Handles the error that occurs in the catch block
99+
* after we try fs.unlink(args.socket).
100+
*
101+
* We extracted into a function so that we could
102+
* test this logic more easily.
103+
*/
104+
export const handleArgsSocketCatchError = (error: any) => {
105+
if (!isNodeJSErrnoException(error) || error.code !== "ENOENT") {
106+
logger.error(error.message ? error.message : error)
107+
}
108+
}

test/unit/node/app.test.ts

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { logger } from "@coder/logger"
22
import * as http from "http"
3-
import { createApp, ensureAddress, handleServerError } from "../../../src/node/app"
3+
import { createApp, ensureAddress, handleArgsSocketCatchError, handleServerError } from "../../../src/node/app"
44
import { setDefaults } from "../../../src/node/cli"
55
import { getAvailablePort } from "../../utils/helpers"
66

@@ -167,10 +167,75 @@ describe("handleServerError", () => {
167167

168168
// make the socket on the file path and i'll get that..
169169

170-
// pass in / into
171170
// create a directory and pass that in as the socket
172171
// with one file and use the directory as the socket path
173172

174173
// The other thing I can do is mock fs.unlink
175174
// and make it throw an error
176-
// Stopped
175+
// Stopped
176+
177+
describe("handleArgsSocketCatchError", () => {
178+
let spy: jest.SpyInstance
179+
180+
beforeEach(() => {
181+
spy = jest.spyOn(logger, "error")
182+
})
183+
184+
afterEach(() => {
185+
jest.clearAllMocks()
186+
})
187+
188+
afterAll(() => {
189+
jest.restoreAllMocks()
190+
})
191+
192+
it("should log an error if its not an isNodeJSErrnoException", () => {
193+
const error = new Error()
194+
195+
handleArgsSocketCatchError(error)
196+
197+
expect(spy).toHaveBeenCalledTimes(1)
198+
expect(spy).toHaveBeenCalledWith(error)
199+
})
200+
201+
it("should log an error if its not an isNodeJSErrnoException (and the error has a message)", () => {
202+
const errorMessage = "handleArgsSocketCatchError Error"
203+
const error = new Error(errorMessage)
204+
205+
handleArgsSocketCatchError(error)
206+
207+
expect(spy).toHaveBeenCalledTimes(1)
208+
expect(spy).toHaveBeenCalledWith(errorMessage)
209+
})
210+
211+
it("should not log an error if its a isNodeJSErrnoException", () => {
212+
const error: NodeJS.ErrnoException = new Error()
213+
error.code = "ENOENT"
214+
215+
handleArgsSocketCatchError(error)
216+
217+
expect(spy).toHaveBeenCalledTimes(0)
218+
})
219+
220+
it("should log an error if the code is not ENOENT (and the error has a message)", () => {
221+
const errorMessage = "no access"
222+
const error: NodeJS.ErrnoException = new Error()
223+
error.code = "EACCESS"
224+
error.message = errorMessage
225+
226+
handleArgsSocketCatchError(error)
227+
228+
expect(spy).toHaveBeenCalledTimes(1)
229+
expect(spy).toHaveBeenCalledWith(errorMessage)
230+
})
231+
232+
it("should log an error if the code is not ENOENT", () => {
233+
const error: NodeJS.ErrnoException = new Error()
234+
error.code = "EACCESS"
235+
236+
handleArgsSocketCatchError(error)
237+
238+
expect(spy).toHaveBeenCalledTimes(1)
239+
expect(spy).toHaveBeenCalledWith(error)
240+
})
241+
})

0 commit comments

Comments
 (0)