Files
TK-FB-Project/api.hyungi.net/middlewares/errorHandler.js
Hyungi Ahn b2461502e7 refactor(phase2-1): 에러 처리 및 로깅 시스템 개선
## 추가된 파일
- utils/errors.js: 표준화된 커스텀 에러 클래스
  - AppError, ValidationError, AuthenticationError
  - ForbiddenError, NotFoundError, ConflictError
  - DatabaseError, ExternalApiError, TimeoutError

- utils/logger.js: 통합 로깅 유틸리티
  - 로그 레벨별 관리 (ERROR, WARN, INFO, DEBUG)
  - 콘솔 및 파일 로깅 지원
  - HTTP 요청/DB 쿼리 전용 로거

## 개선된 파일
- middlewares/errorHandler.js: 에러 핸들러 개선
  - 새로운 AppError 클래스 사용
  - logger 통합
  - asyncHandler 및 notFoundHandler 추가

## 다음 단계
- config 파일들 생성 (cors, security)
- activityLogger 미들웨어 생성
- userController 인라인 코드 분리

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-11 10:28:51 +09:00

102 lines
2.3 KiB
JavaScript

/**
* 에러 핸들러 미들웨어
*
* 애플리케이션 전역 에러를 처리하는 Express 미들웨어
*
* @author TK-FB-Project
* @since 2025-12-11
*/
const { AppError } = require('../utils/errors');
const logger = require('../utils/logger');
/**
* 에러 응답 포맷터
*/
const formatErrorResponse = (error, req) => {
const response = {
success: false,
error: {
message: error.message || '알 수 없는 오류가 발생했습니다',
code: error.code || 'INTERNAL_ERROR'
},
timestamp: new Date().toISOString()
};
// 검증 에러의 경우 상세 정보 포함
if (error.details) {
response.error.details = error.details;
}
// 개발 환경에서만 스택 트레이스 포함
if (process.env.NODE_ENV === 'development') {
response.error.stack = error.stack;
response.request = {
method: req.method,
url: req.originalUrl,
ip: req.ip,
user: req.user?.username || 'anonymous'
};
}
return response;
};
/**
* 에러 핸들러 미들웨어
*/
const errorHandler = (error, req, res, next) => {
// AppError가 아닌 경우 변환
if (!(error instanceof AppError)) {
error = new AppError(
error.message || '서버 내부 오류가 발생했습니다',
500,
'INTERNAL_ERROR'
);
}
// 로깅
if (error.statusCode >= 500) {
logger.error(error.message, {
code: error.code,
stack: error.stack,
url: req.originalUrl,
method: req.method,
user: req.user?.username || 'anonymous'
});
} else {
logger.warn(error.message, {
code: error.code,
url: req.originalUrl,
method: req.method,
user: req.user?.username || 'anonymous'
});
}
// 응답
const response = formatErrorResponse(error, req);
res.status(error.statusCode).json(response);
};
/**
* 비동기 함수 래퍼 (에러 자동 처리)
*/
const asyncHandler = (fn) => {
return (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
};
/**
* 404 Not Found 핸들러
*/
const notFoundHandler = (req, res, next) => {
const { NotFoundError } = require('../utils/errors');
next(new NotFoundError(`경로를 찾을 수 없습니다: ${req.originalUrl}`));
};
module.exports = {
errorHandler,
asyncHandler,
notFoundHandler
};