Skip to content

Commit 3452ba4

Browse files
committed
feat: 예외 처리
1 parent 7dfd8c5 commit 3452ba4

File tree

6 files changed

+69
-8
lines changed

6 files changed

+69
-8
lines changed

src/main/java/roomescape/controller/HomeController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class HomeController {
88

99
@RequestMapping("/")
10-
public String home(){
10+
public String home() {
1111
return "home";
1212
}
1313

src/main/java/roomescape/controller/ReservationController.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.springframework.web.bind.annotation.RequestBody;
1010
import org.springframework.web.bind.annotation.ResponseBody;
1111
import roomescape.dto.Reservation;
12+
import roomescape.exception.InvalidValueException;
13+
import roomescape.exception.NotFoundReservationException;
1214

1315
import java.net.URI;
1416
import java.util.ArrayList;
@@ -20,7 +22,7 @@ public class ReservationController {
2022

2123
private List<Reservation> reservations = new ArrayList<>();
2224
private AtomicLong index = new AtomicLong(1);
23-
25+
// 이단계
2426
// public ReservationController() {
2527
// reservations.add(new Reservation(1L, "브라운1", "2023-01-01", "10:00"));
2628
// reservations.add(new Reservation(2L, "브라운2", "2023-01-02", "11:00"));
@@ -29,36 +31,41 @@ public class ReservationController {
2931

3032
// 홈화면
3133
@GetMapping("/reservation")
32-
public String reservationPage(){
34+
public String reservationPage() {
3335
return "reservation";
3436
}
3537

3638
//예약 조회
3739
@ResponseBody
3840
@GetMapping("/reservations")
39-
public List<Reservation> list(){
41+
public List<Reservation> list() {
4042
return reservations;
4143
}
4244

4345
//예약 추가
4446
@ResponseBody
4547
@PostMapping("/reservations")
46-
public ResponseEntity<Reservation> create(@RequestBody Reservation newReservation){
48+
public ResponseEntity<Reservation> create(@RequestBody Reservation newReservation) {
49+
if (newReservation.getName() == null || newReservation.getName().isEmpty() ||
50+
newReservation.getDate() == null || newReservation.getDate().isEmpty() ||
51+
newReservation.getTime() == null || newReservation.getTime().isEmpty()) {
52+
throw new InvalidValueException("예약 추가에 필요한 인자값이 비어있습니다.");
53+
}
4754

4855
Reservation reservation = new Reservation(index.getAndIncrement(), newReservation.getName(), newReservation.getDate(), newReservation.getTime());
49-
5056
reservations.add(reservation);
57+
5158
return ResponseEntity.created(URI.create("/reservations/" + reservation.getId()))
5259
.body(reservation);
5360
}
5461

5562
//예약 삭제
5663
@DeleteMapping("/reservations/{id}")
57-
public ResponseEntity<Void> delete(@PathVariable Long id){
64+
public ResponseEntity<Void> delete(@PathVariable Long id) {
5865
Reservation reservation = reservations.stream()
5966
.filter(it -> it.getId().equals(id))
6067
.findFirst()
61-
.orElseThrow(() -> new RuntimeException("Reservation not found"));
68+
.orElseThrow(() -> new NotFoundReservationException("예약을 찾을 수 없습니다."));
6269

6370
reservations.remove(reservation);
6471
return ResponseEntity.noContent().build();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package roomescape.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.RestControllerAdvice;
6+
7+
@RestControllerAdvice
8+
public class ExceptionHandler {
9+
10+
@org.springframework.web.bind.annotation.ExceptionHandler(NotFoundReservationException.class)
11+
public ResponseEntity<String> handleNotFoundReservationException(NotFoundReservationException ex) {
12+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
13+
}
14+
15+
@org.springframework.web.bind.annotation.ExceptionHandler(InvalidValueException.class)
16+
public ResponseEntity<String> handleInvalidReservationException(InvalidValueException ex) {
17+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
18+
}
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package roomescape.exception;
2+
3+
public class InvalidValueException extends RuntimeException {
4+
5+
public InvalidValueException(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package roomescape.exception;
2+
3+
public class NotFoundReservationException extends RuntimeException {
4+
public NotFoundReservationException(String message) {
5+
super(message);
6+
}
7+
}

src/test/java/roomescape/MissionStepTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,25 @@ public class MissionStepTest {
7171
.body("size()", is(0));
7272
}
7373

74+
@Test
75+
void 사단계() {
76+
Map<String, String> params = new HashMap<>();
77+
params.put("name", "브라운");
78+
params.put("date", "");
79+
params.put("time", "");
7480

81+
// 필요한 인자가 없는 경우
82+
RestAssured.given().log().all()
83+
.contentType(ContentType.JSON)
84+
.body(params)
85+
.when().post("/reservations")
86+
.then().log().all()
87+
.statusCode(400);
88+
89+
// 삭제할 예약이 없는 경우
90+
RestAssured.given().log().all()
91+
.when().delete("/reservations/1")
92+
.then().log().all()
93+
.statusCode(400);
94+
}
7595
}

0 commit comments

Comments
 (0)