@@ -2,8 +2,9 @@ import { logger } from "@coder/logger"
2
2
import express from "express"
3
3
import * as http from "http"
4
4
import * as path from "path"
5
- import { HttpCode } from "../common/http"
5
+ import { HttpCode , HttpError } from "../common/http"
6
6
import { listen } from "./app"
7
+ import { errorHandler } from "./routes/errors"
7
8
import { canConnect } from "./util"
8
9
9
10
export interface EditorSessionEntry {
@@ -44,24 +45,18 @@ export async function makeEditorSessionManagerServer(
44
45
async ( req , res ) => {
45
46
const filePath = req . query . filePath
46
47
if ( ! filePath ) {
47
- res . status ( HttpCode . BadRequest ) . send ( "filePath is required" )
48
- return
49
- }
50
- try {
51
- const socketPath = await editorSessionManager . getConnectedSocketPath ( filePath )
52
- const response : GetSessionResponse = { socketPath }
53
- res . json ( response )
54
- } catch ( error : unknown ) {
55
- res . status ( HttpCode . ServerError ) . send ( error )
48
+ throw new HttpError ( "filePath is required" , HttpCode . BadRequest )
56
49
}
50
+ const socketPath = await editorSessionManager . getConnectedSocketPath ( filePath )
51
+ const response : GetSessionResponse = { socketPath }
52
+ res . json ( response )
57
53
} ,
58
54
)
59
55
60
56
router . post < { } , string , AddSessionRequest | undefined > ( "/add-session" , async ( req , res ) => {
61
57
const entry = req . body ?. entry
62
58
if ( ! entry ) {
63
- res . status ( 400 ) . send ( "entry is required" )
64
- return
59
+ throw new HttpError ( "entry is required" , HttpCode . BadRequest )
65
60
}
66
61
editorSessionManager . addSession ( entry )
67
62
res . status ( 200 ) . send ( "session added" )
@@ -70,13 +65,14 @@ export async function makeEditorSessionManagerServer(
70
65
router . post < { } , string , DeleteSessionRequest | undefined > ( "/delete-session" , async ( req , res ) => {
71
66
const socketPath = req . body ?. socketPath
72
67
if ( ! socketPath ) {
73
- res . status ( 400 ) . send ( "socketPath is required" )
74
- return
68
+ throw new HttpError ( "socketPath is required" , HttpCode . BadRequest )
75
69
}
76
70
editorSessionManager . deleteSession ( socketPath )
77
71
res . status ( 200 ) . send ( "session deleted" )
78
72
} )
79
73
74
+ router . use ( errorHandler )
75
+
80
76
const server = http . createServer ( router )
81
77
try {
82
78
await listen ( server , { socket : codeServerSocketPath } )
0 commit comments