Use error handler in session server (#7455)

This commit is contained in:
Asher 2025-08-04 11:05:48 -08:00 committed by GitHub
parent bc15fa461c
commit b5a2ce2522
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,8 +2,9 @@ import { logger } from "@coder/logger"
import express from "express" import express from "express"
import * as http from "http" import * as http from "http"
import * as path from "path" import * as path from "path"
import { HttpCode } from "../common/http" import { HttpCode, HttpError } from "../common/http"
import { listen } from "./app" import { listen } from "./app"
import { errorHandler } from "./routes/errors"
import { canConnect } from "./util" import { canConnect } from "./util"
export interface EditorSessionEntry { export interface EditorSessionEntry {
@ -44,24 +45,18 @@ export async function makeEditorSessionManagerServer(
async (req, res) => { async (req, res) => {
const filePath = req.query.filePath const filePath = req.query.filePath
if (!filePath) { if (!filePath) {
res.status(HttpCode.BadRequest).send("filePath is required") throw new HttpError("filePath is required", HttpCode.BadRequest)
return
}
try {
const socketPath = await editorSessionManager.getConnectedSocketPath(filePath)
const response: GetSessionResponse = { socketPath }
res.json(response)
} catch (error: unknown) {
res.status(HttpCode.ServerError).send(error)
} }
const socketPath = await editorSessionManager.getConnectedSocketPath(filePath)
const response: GetSessionResponse = { socketPath }
res.json(response)
}, },
) )
router.post<{}, string, AddSessionRequest | undefined>("/add-session", async (req, res) => { router.post<{}, string, AddSessionRequest | undefined>("/add-session", async (req, res) => {
const entry = req.body?.entry const entry = req.body?.entry
if (!entry) { if (!entry) {
res.status(400).send("entry is required") throw new HttpError("entry is required", HttpCode.BadRequest)
return
} }
editorSessionManager.addSession(entry) editorSessionManager.addSession(entry)
res.status(200).send("session added") res.status(200).send("session added")
@ -70,13 +65,14 @@ export async function makeEditorSessionManagerServer(
router.post<{}, string, DeleteSessionRequest | undefined>("/delete-session", async (req, res) => { router.post<{}, string, DeleteSessionRequest | undefined>("/delete-session", async (req, res) => {
const socketPath = req.body?.socketPath const socketPath = req.body?.socketPath
if (!socketPath) { if (!socketPath) {
res.status(400).send("socketPath is required") throw new HttpError("socketPath is required", HttpCode.BadRequest)
return
} }
editorSessionManager.deleteSession(socketPath) editorSessionManager.deleteSession(socketPath)
res.status(200).send("session deleted") res.status(200).send("session deleted")
}) })
router.use(errorHandler)
const server = http.createServer(router) const server = http.createServer(router)
try { try {
await listen(server, { socket: codeServerSocketPath }) await listen(server, { socket: codeServerSocketPath })