Skip to content

Commit d9bb5d5

Browse files
김산김산
authored andcommitted
[#7] Feat: exception handling
1 parent f9801fd commit d9bb5d5

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/main/java/com/example/helper/controller/MealController.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
import lombok.extern.slf4j.Slf4j;
1111
import org.springframework.beans.BeanUtils;
1212
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.http.HttpStatus;
1314
import org.springframework.web.bind.annotation.*;
1415

1516
import java.util.ArrayList;
1617
import java.util.HashMap;
1718
import java.util.List;
1819
import java.util.Map;
20+
import org.springframework.web.server.ResponseStatusException;
1921

2022

2123
@RestController // @Controller + @ResponseBody. return이 view가 아닌, http body에 직접 쓰여짐.
@@ -253,8 +255,12 @@ public DateMealDto DateMealRead(
253255
.date(day.toString())
254256
.build();
255257

256-
DateMealDto dateMealDtoList = dateMealService.getDateMenus(dateReqDto);
257-
258-
return dateMealDtoList;
258+
try {
259+
DateMealDto dateMealDto = dateMealService.getDateMenus(dateReqDto);
260+
return dateMealDto;
261+
} catch (IllegalStateException e) {
262+
throw new ResponseStatusException(
263+
HttpStatus.NOT_ACCEPTABLE, e.getMessage());
264+
}
259265
}
260266
}

src/main/java/com/example/helper/service/DateMealService.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public DateMealDto getDateMenus(DateReqDto dateReqDto) {
2828
.build();
2929

3030
// TODO: exception handling(target data is not in DB)
31-
31+
if (checkMenusEmpty(result)) {
32+
throw new IllegalStateException("해당 날짜의 식단이 존재하지 않습니다.");
33+
}
3234
return result;
3335
}
3436

@@ -71,4 +73,11 @@ public String padZero(String str) {
7173
}
7274
return result;
7375
}
76+
77+
public boolean checkMenusEmpty(DateMealDto dateMealDto) {
78+
return dateMealDto.getBreakfast().isEmpty() &&
79+
dateMealDto.getLunch().isEmpty() &&
80+
dateMealDto.getDinner().isEmpty() &&
81+
dateMealDto.getLunch_corner().isEmpty();
82+
}
7483
}

0 commit comments

Comments
 (0)