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

+19 -28
View File
@@ -1,8 +1,9 @@
import { matchedData } from 'express-validator'
import { User } from '@linux-odyssey/models'
import { genJWT, hashPassword } from '../../utils/auth.js'
import { asyncHandler } from '../../middleware/error.js'
export async function issueToken(req, res) {
export const issueToken = asyncHandler(async (req, res) => {
const { user } = req
const token = await genJWT({
_id: user.id,
@@ -10,34 +11,27 @@ export async function issueToken(req, res) {
email: user.email,
})
res.json({ token })
}
})
export async function register(req, res, next) {
export const register = asyncHandler(async (req, res, next) => {
const { username, password, email } = matchedData(req)
const user = new User({
username,
email,
})
try {
user.hashedPassword = await hashPassword(password)
await user.save()
req.login(user, (err) => {
if (err) {
next(err)
return
}
res.status(201).json({
message: 'user created',
})
user.hashedPassword = await hashPassword(password)
await user.save()
req.login(user, (err) => {
if (err) {
next(err)
return
}
res.status(201).json({
message: 'user created',
})
} catch (err) {
console.error(err)
res.status(500).json({
message: 'error registering user',
})
}
}
})
})
export function logout(req, res) {
req.logout((err) => {
@@ -60,7 +54,7 @@ export function checkSession(req, res) {
})
}
export async function socialLogin(req, res) {
export function socialLogin(req, res) {
const { newUser } = req.user
if (newUser) {
req.session.newUser = newUser
@@ -70,7 +64,7 @@ export async function socialLogin(req, res) {
res.redirect('/')
}
export async function registerFromSession(req, res) {
export const registerFromSession = asyncHandler(async (req, res, next) => {
const { username } = matchedData(req)
const { newUser } = req.session
@@ -88,10 +82,7 @@ export async function registerFromSession(req, res) {
req.login(user, (err) => {
if (err) {
console.error(err)
res.status(500).json({
message: 'error logging in',
})
next(err)
return
}
delete req.session.newUser
@@ -99,4 +90,4 @@ export async function registerFromSession(req, res) {
message: 'user created',
})
})
}
})
@@ -1,27 +1,21 @@
import { Command, Session } from '@linux-odyssey/models'
import { matchedData } from 'express-validator'
import CommandHandler from '../../game/commandHandler.js'
import { pushToSession } from '../socket.js'
import { finishSession } from '../../game/sessionManager.js'
import { asyncHandler } from '../../middleware/error.js'
const commandCompleteCallbacks = new Map()
export async function newCommand(req, res) {
const { command, pwd, output, error, ...additionalData } = req.body
if (!command) {
res.status(400).json({ message: 'command is required' })
return
}
export const newCommand = asyncHandler(async (req, res) => {
const { command, pwd, output, error, params } = matchedData(req)
const { sessionId } = req.user
const session = await Session.findById(sessionId)
if (!session) {
res.status(404).json({ message: 'session not found' })
return
}
if (session.status === 'finished') {
// Allow commands to be sent to finished sessions
res.status(200).end()
@@ -44,10 +38,8 @@ export async function newCommand(req, res) {
await c.save()
const commandHandler = new CommandHandler(session, c, additionalData)
const commandHandler = new CommandHandler(session, c, params)
const response = await commandHandler.run()
if (response) {
c.stage = response.stage
await c.save()
@@ -61,9 +53,9 @@ export async function newCommand(req, res) {
res.status(201).json(response)
await session.save()
}
})
export async function completedCommand(req, res) {
export const completedCommand = asyncHandler(async (req, res) => {
const { sessionId } = req.user
const session = await Session.findById(sessionId)
@@ -86,4 +78,4 @@ export async function completedCommand(req, res) {
callback()
commandCompleteCallbacks.delete(session.id)
res.json({ message: 'ok' })
}
})
@@ -1,7 +1,8 @@
import { Quest } from '@linux-odyssey/models'
import { matchedData } from 'express-validator'
import { asyncHandler } from '../../middleware/error.js'
export async function getQuests(req, res) {
export const getQuests = asyncHandler(async (req, res) => {
const quests = await Quest.find({})
res.json(
quests.map(({ title, _id, requirements }) => ({
@@ -10,9 +11,9 @@ export async function getQuests(req, res) {
requirements,
}))
)
}
})
export async function getQuestDetail(req, res) {
export const getQuestDetail = asyncHandler(async (req, res) => {
const { questId } = matchedData(req)
const quest = await Quest.findById(questId)
if (!quest) {
@@ -21,4 +22,4 @@ export async function getQuestDetail(req, res) {
}
const { _id, title, order, instruction } = quest
res.json({ _id, title, order, instruction })
}
})
+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))
})
+9 -9
View File
@@ -1,20 +1,20 @@
import { UserProfile } from '@linux-odyssey/models'
import { asyncHandler } from '../../middleware/error.js'
export async function me(req, res) {
export const me = asyncHandler(async (req, res) => {
const userProfile = await UserProfile.findOne({
user: req.user._id,
}).populate('user')
})
if (!userProfile) {
res.status(404).json({ message: 'User not found' })
return
throw new Error(
`UserProfile not found for user ${req.user.username} ${req.user._id}!`
)
}
const {
user: { username, email },
progress,
} = userProfile
const { username, email } = req.user
const { progress } = userProfile
res.json({
username,
email,
progress,
})
}
})