fix: error handling

- Remove unnecessary try-catch block in createSession function
- Add error handling for missing session ID in socket.js
- Improve error handling in connectDB function
- Add error handling middleware for server errors
- Add default export for get function in env.js

Signed-off-by: wancat <wancat@wancat.cc>
This commit is contained in:
wancat committed 2023-07-25 15:46:59 +08:00
1 parent 943c11714c
commit 88ed447573
6 files changed
+36 -14

No files matched your search

@@ -22,13 +22,13 @@ export async function createSession(req, res) {
return
}
const newSession = new Session({
user: req.user,
quest,
containerId: `quest-${quest.id}-${req.user.username}-${Date.now()}`,
})
try {
const newSession = new Session({
user: req.user,
quest,
containerId: `quest-${quest.id}-${req.user.username}-${Date.now()}`,
})
const session = await newSession.save()
res.status(201).json(session)
} catch (err) {
+19 -7
View File
@@ -21,13 +21,25 @@ export default (server) => {
io.on('connection', async (socket) => {
console.log('Connected to the client.')
const session = await Session.findOne({
_id: socket.handshake.query.session_id,
user: socket.user,
})
if (!session) {
socket.send('Session not found.')
const sessionId = socket.handshake.query.session_id
if (!sessionId) {
socket.send('Session ID not found.')
socket.disconnect()
return
}
let session
try {
session = await Session.findOne({
_id: sessionId,
user: socket.user,
})
if (!session) {
socket.send('Session not found.')
socket.disconnect()
return
}
} catch (err) {
socket.send(err.message)
socket.disconnect()
return
}