try-catch를 사용하여 오류 발생 시 Error를 throw
로깅을 통해 에러를 기록 (console.error) - 서버
컨트롤러단에서 에러 메시지를 받아 적절히 처리
DB 쿼리 실패 시 throw new Error("에러 메세지")
서비스에서 catch
Repository 에러를 받아 추가 검증 후 다시 throw
게시글이 없으면 throw new Error("에러 메세지")
컨트롤러에서 catch
Service에서 던진 에러를 catch
클라이언트에게 HTTP 500 응답 반환
글로벌 에러 핸들러 미들웨어를 추가하면 더 효율적이다
const errorMiddleware = (err, req, res, next) => {
console.error("[Error] 서버 에러:", err);
res.status(500).json({ success: false, msg: err.message || "서버 오류 발생" });
};