mirror of
https://github.com/cdr/code-server.git
synced 2025-12-06 08:27:17 +01:00
Use error handler in session server (#7455)
This commit is contained in:
parent
bc15fa461c
commit
b5a2ce2522
1 changed files with 10 additions and 14 deletions
|
|
@ -2,8 +2,9 @@ import { logger } from "@coder/logger"
|
|||
import express from "express"
|
||||
import * as http from "http"
|
||||
import * as path from "path"
|
||||
import { HttpCode } from "../common/http"
|
||||
import { HttpCode, HttpError } from "../common/http"
|
||||
import { listen } from "./app"
|
||||
import { errorHandler } from "./routes/errors"
|
||||
import { canConnect } from "./util"
|
||||
|
||||
export interface EditorSessionEntry {
|
||||
|
|
@ -44,24 +45,18 @@ export async function makeEditorSessionManagerServer(
|
|||
async (req, res) => {
|
||||
const filePath = req.query.filePath
|
||||
if (!filePath) {
|
||||
res.status(HttpCode.BadRequest).send("filePath is required")
|
||||
return
|
||||
throw new HttpError("filePath is required", HttpCode.BadRequest)
|
||||
}
|
||||
try {
|
||||
const socketPath = await editorSessionManager.getConnectedSocketPath(filePath)
|
||||
const response: GetSessionResponse = { socketPath }
|
||||
res.json(response)
|
||||
} catch (error: unknown) {
|
||||
res.status(HttpCode.ServerError).send(error)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
router.post<{}, string, AddSessionRequest | undefined>("/add-session", async (req, res) => {
|
||||
const entry = req.body?.entry
|
||||
if (!entry) {
|
||||
res.status(400).send("entry is required")
|
||||
return
|
||||
throw new HttpError("entry is required", HttpCode.BadRequest)
|
||||
}
|
||||
editorSessionManager.addSession(entry)
|
||||
res.status(200).send("session added")
|
||||
|
|
@ -70,13 +65,14 @@ export async function makeEditorSessionManagerServer(
|
|||
router.post<{}, string, DeleteSessionRequest | undefined>("/delete-session", async (req, res) => {
|
||||
const socketPath = req.body?.socketPath
|
||||
if (!socketPath) {
|
||||
res.status(400).send("socketPath is required")
|
||||
return
|
||||
throw new HttpError("socketPath is required", HttpCode.BadRequest)
|
||||
}
|
||||
editorSessionManager.deleteSession(socketPath)
|
||||
res.status(200).send("session deleted")
|
||||
})
|
||||
|
||||
router.use(errorHandler)
|
||||
|
||||
const server = http.createServer(router)
|
||||
try {
|
||||
await listen(server, { socket: codeServerSocketPath })
|
||||
|
|
|
|||
Loading…
Reference in a new issue