diff --git a/src/common/util.ts b/src/common/util.ts index b4f66be24..87ca6f596 100644 --- a/src/common/util.ts +++ b/src/common/util.ts @@ -112,3 +112,11 @@ export const getFirstString = (value: string | string[] | object | undefined): s return typeof value === "string" ? value : undefined } + +export function logError(prefix: string, err: any): void { + if (err instanceof Error) { + logger.error(`${prefix}: ${err.message} ${err.stack}`) + } else { + logger.error(`${prefix}: ${err}`) + } +} diff --git a/src/node/app.ts b/src/node/app.ts index 448ec9660..1d9ce9d4a 100644 --- a/src/node/app.ts +++ b/src/node/app.ts @@ -3,6 +3,7 @@ import express, { Express } from "express" import { promises as fs } from "fs" import http from "http" import * as httpolyglot from "httpolyglot" +import * as util from "../common/util" import { DefaultedArgs } from "./cli" import { handleUpgrade } from "./wsRouter" @@ -22,8 +23,21 @@ export const createApp = async (args: DefaultedArgs): Promise<[Express, Express, ) : http.createServer(app) - await new Promise(async (resolve, reject) => { - server.on("error", reject) + let resolved = false + await new Promise(async (resolve2, reject) => { + const resolve = () => { + resolved = true + resolve2() + } + server.on("error", (err) => { + if (!resolved) { + reject(err) + } else { + // Promise resolved earlier so this is an unrelated error. + util.logError("http server error", err) + } + }) + if (args.socket) { try { await fs.unlink(args.socket) diff --git a/test/testutil.ts b/test/testutil.ts index 561662758..dc88da3db 100644 --- a/test/testutil.ts +++ b/test/testutil.ts @@ -1,7 +1,7 @@ import * as http from "http" -import { logger } from "@coder/logger" -import { ensureAddress } from "../src/node/app" import * as nodeFetch from "node-fetch" +import * as util from "../src/common/util" +import { ensureAddress } from "../src/node/app" export class HttpServer { private hs = http.createServer() @@ -25,7 +25,7 @@ export class HttpServer { rej(err) } else { // Promise resolved earlier so this is some other error. - logError("server error", err) + util.logError("http server error", err) } }) }) @@ -54,12 +54,3 @@ export class HttpServer { return nodeFetch.default(`${ensureAddress(this.hs)}${requestPath}`, opts) } } - - -export function logError(prefix: string, err: any): void { - if (err instanceof Error) { - logger.error(`${prefix}: ${err.message} ${err.stack}`) - } else { - logger.error(`${prefix}: ${err}`) - } -}