fix: wrap all async controller with error handler

* add validations to newCommand
This commit is contained in:
wancat authored and Lin, Hung-Shin committed 2023-11-14 10:45:24 +08:00
1 parent 1af85651b8
commit c2dac8d385
13 files changed
+120 -120

No files matched your search

+31 -49
View File
@@ -4,6 +4,7 @@ import {
createNewSession,
getOrCreateActiveSession,
} from '../../game/sessionManager.js'
import { asyncHandler } from '../../middleware/error.js'
function sessionSummary(session) {
return {
@@ -25,59 +26,40 @@ function sessionDetail(session) {
}
}
export async function getSessionList(req, res) {
try {
const query = { user: req.user._id }
const { questId, status } = matchedData(req)
if (questId) {
query.quest = questId
}
query.status = status || 'active'
const sessions = await Session.find(query)
res.json(sessions.map(sessionSummary))
} catch (err) {
console.error(err)
res.status(500).json({ message: err.message })
export const getSessionList = asyncHandler(async (req, res) => {
const query = { user: req.user._id }
const { questId, status } = matchedData(req)
if (questId) {
query.quest = questId
}
}
query.status = status || 'active'
const sessions = await Session.find(query)
res.json(sessions.map(sessionSummary))
})
export async function createSession(req, res) {
export const createSession = asyncHandler(async (req, res) => {
const { questId } = matchedData(req)
try {
const session = await createNewSession(req.user, questId)
res.status(201).json(sessionDetail(session))
} catch (err) {
console.error(err)
res.status(500).json({ message: err.message })
const session = await createNewSession(req.user, questId)
res.status(201).json(sessionDetail(session))
})
export const getSessionById = asyncHandler(async (req, res) => {
const { sessionId } = matchedData(req)
const session = await Session.findOne({
_id: sessionId,
user: req.user._id,
})
if (!session) {
res.status(404).json({ message: 'Session not found.' })
return
}
}
export async function getSessionById(req, res) {
try {
const { sessionId } = matchedData(req)
const session = await Session.findOne({
_id: sessionId,
user: req.user._id,
})
res.json(sessionDetail(session))
})
if (!session) {
res.status(404).json({ message: 'Session not found.' })
return
}
res.json(sessionDetail(session))
} catch (err) {
res.status(500).json({ message: err.message })
}
}
export async function getOrCreateSession(req, res) {
export const getOrCreateSession = asyncHandler(async (req, res) => {
const { questId } = matchedData(req)
try {
const session = await getOrCreateActiveSession(req.user, questId)
res.json(sessionDetail(session))
} catch (err) {
console.error(err)
res.status(500).json({ message: err.message })
}
}
const session = await getOrCreateActiveSession(req.user, questId)
res.json(sessionDetail(session))
})