@ExceptionHandler(AccountNotFoundException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ErrorResponse handleAccountNOtFoundException(AccountNotFoundException e) { ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setMessage("[" + e.getId() + "]에 대당하는 계정이 없습니다."); errorResponse.setCode("account.not.found.exception"); return errorResponse; }
// TODO 예외 처리 네번째 방법 (콜백 비스무리한거....) @ExceptionHandler(UserDuplicaredException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ErrorResponse handleUserDuplicatedException(UserDuplicaredException e) { ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setMessage("[" + e.getUsername() + "] 중복된 username 입니다."); errorResponse.setCode("duplicated.username.exception"); return errorResponse; }
@RequestMapping(value = "/accounts", method = POST) public ResponseEntity createAccount( @RequestBody @Valid AccountDto.Create create, BindingResult result) { if (result.hasErrors()) { ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setMessage("잘못된 요청입니다."); errorResponse.setCode("bad.request"); // TODO bindingResult 안에 들어있는 에러 정보 사용하기. return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); } // 1.리턴 타임으로 판단. // 2.파라미터 이용. Account newAccount = service.createAccount(create); return new ResponseEntity<>( modelMapper.map(newAccount, AccountDto.Response.class), HttpStatus.CREATED); }