app.ts: Fix createApp to log all http server errors

cc @code-asher
This commit is contained in:
Anmol Sethi 2021-01-14 09:49:23 -05:00
parent 8acb2aec11
commit d3074278ca
No known key found for this signature in database
GPG Key ID: 8CEF1878FF10ADEB
3 changed files with 27 additions and 14 deletions

View File

@ -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}`)
}
}

View File

@ -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<http.Server>(async (resolve, reject) => {
server.on("error", reject)
let resolved = false
await new Promise<http.Server>(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)

View File

@ -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}`)
}
}