diff --git a/index.js b/index.js index 3a17391..88ccac8 100644 --- a/index.js +++ b/index.js @@ -75,15 +75,22 @@ app.use((req, res, next) => { app.use((err, req, res, next) => { res.locals.message = err.message; res.locals.error = process.env.NODE_ENV !== "production" ? err : {}; - - // 상태 코드가 있으면 사용, 없으면 500으로 설정 + const statusCode = err.status || status.INTERNAL_SERVER_ERROR; + console.error("Error Stack:", err.stack); console.error("Error Data:", err.data); - res - .status(statusCode) - .send(response(err.data || { message: "Internal Server Error" })); -}); + + // 응답은 response()로 포맷팅하되, statusCode는 숫자로 분리 + const responseBody = response(err.data || { + code: 'COMMON000', + isSuccess: false, + message: 'Internal Server Error', + result: null, + }); + + res.status(statusCode).json(responseBody); // ✅ send → json도 좋음 + }); //sample app.listen(app.get("port"), () => { diff --git a/src/controllers/music.controller.js b/src/controllers/music.controller.js index 9fc3b64..3d99bc4 100644 --- a/src/controllers/music.controller.js +++ b/src/controllers/music.controller.js @@ -30,19 +30,23 @@ export const insertMusicController=async(req,res,next)=>{ // music info 불러오는 Controller export const musicInfoController = async (req, res, next) => { try { - const token = req.headers.authorization.split(' ')[1];; - console.log(token); - const decoded = jwt.decode(token); - console.log(decoded); - const userId = decoded.req.id; - const songId = req.params.id; - const musicInfo = await musicInfoService(userId, songId); - res.send(response(status.SUCCESS, musicInfo)); + const token = req.headers.authorization?.split(' ')[1]; + const decoded = jwt.decode(token); + const userId = decoded?.id; + const songId = req.params.id; + + if (!userId) throw new BaseError(status.UNAUTHORIZED); + + const musicInfo = await musicInfoService(userId, songId); + res.status(status.SUCCESS.status).json(response(status.SUCCESS, musicInfo)); + } catch (error) { - console.error(error); - res.send(response(status.BAD_REQUEST, BaseError(status.BAD_REQUEST))); + console.error(error); + + const errStatus = error instanceof BaseError ? error.status : status.BAD_REQUEST.status; + res.status(errStatus).json(response(status.BAD_REQUEST)); } -}; + }; // music change-info(update) Controller