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 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 socketPath = await editorSessionManager.getConnectedSocketPath(filePath)
|
||||||
const response: GetSessionResponse = { socketPath }
|
const response: GetSessionResponse = { socketPath }
|
||||||
res.json(response)
|
res.json(response)
|
||||||
} catch (error: unknown) {
|
|
||||||
res.status(HttpCode.ServerError).send(error)
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
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 })
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue