node.js(2)
-
[백엔드]error 처리
기본적인 crud를 다 구현하고 Postman으로 테스트할때 만약 에러가 발생했을 시 어떠한 이유때문에 발생한건지예외처리는 필수다. 안그러면 하나하나 로그를 다 찍으면서 계속 서치해야하기 때문이다. 나는 서비스 레이어에서 HttpException라는 커스텀 에러 클래스를 사용해 에러를 던져줬다.async getUser(id: string): Promise { const user = await this._userRepository.findById(id); if (!user) throw new HttpException(404, "해당 유저가 없습니다."); const dtoUser = await new GetUserResponseDTO(user); return dtoUse..
2025.01.10 -
[백엔드] next(Error)란?
백엔드를 하다보면 예외처리를 필수로 해줘야 한다.주로 try & catch문으로 성공과 실패 처리 로직을 작성해준다.next.js와 node.js를 이용해서 백엔드를 구현했을때를 살펴보자. next(Error)란?-> Express의 에러 처리 미들웨어로 에러를 전달하는 방식 주로 컨트롤러 레이어에서 사용한다.예를 들어 인증 api를 구현할때 /api/auth/controller/auth.controller.tsasync login(req: Request, res: Response, next: NextFunction) { try { const { loginId, password } = req.body; const result = await this._authService.login..
2025.01.07