Skip to content

Commit 8b3cda0

Browse files
committed
Add some more realistic exceptions using thrown exceptions for defense
1 parent 6748f2a commit 8b3cda0

File tree

6 files changed

+20
-28
lines changed

6 files changed

+20
-28
lines changed

src/main/java/org/springframework/samples/petclinic/model/package-info.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ private String addPaginationModel(int page, Model model, Page<Owner> paginated)
136136
}
137137

138138
private Page<Owner> findPaginatedForOwnersLastName(int page, String lastname) {
139+
if(page < 1){
140+
// defensive programming
141+
throw new IllegalArgumentException("Page must be greater than 0");
142+
}
139143

140144
int pageSize = 5;
141145
Pageable pageable = PageRequest.of(page - 1, pageSize);

src/main/java/org/springframework/samples/petclinic/owner/PetController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult res
124124
@GetMapping("/pets/{petId}/edit")
125125
public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, ModelMap model,
126126
RedirectAttributes redirectAttributes) {
127+
if(owner == null){
128+
throw new IllegalArgumentException("Owner not found!");
129+
}
127130
Pet pet = owner.getPet(petId);
131+
if(pet == null){
132+
throw new IllegalArgumentException("Pet not found!");
133+
}
128134
model.put("pet", pet);
129135
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
130136
}

src/main/java/org/springframework/samples/petclinic/vet/VetController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ private String addPaginationModel(int page, Page<Vet> paginated, Model model) {
6969
}
7070

7171
private Page<Vet> findPaginated(int page) {
72+
if(page < 1) {
73+
// defensive programming
74+
throw new IllegalArgumentException("Page must be greater than 0");
75+
}
7276
int pageSize = 5;
7377
Pageable pageable = PageRequest.of(page - 1, pageSize);
7478
return vetRepository.findAll(pageable);
@@ -89,7 +93,6 @@ private Page<Vet> findPaginated(int page) {
8993

9094
@GetMapping("/vets/{lastName}")
9195
public @ResponseBody Vets showResourcesVetList(@PathVariable(name = "lastName") String lastName) {
92-
// This will throw when exercised from tester.sh as the Vet we send in has no specialties. Poor guy.
9396
Vets vets = new Vets();
9497
Collection<Vet> vetList = this.vetRepository.findByLastName(lastName);
9598
for (Vet vet : vetList) {

src/main/resources/templates/fragments/layout.html

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@
5454
<span class="fa fa-th-list" aria-hidden="true"></span>
5555
<span>Veterinarians</span>
5656
</li>
57-
58-
<li
59-
th:replace="~{::menuItem ('/oups','error','trigger a RuntimeException to see how it is handled','exclamation-triangle','Error')}">
60-
<span class="fa exclamation-triangle" aria-hidden="true"></span>
61-
<span>Error</span>
62-
</li>
63-
6457
</ul>
6558
</div>
6659
</div>

tester.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ while true; do
2727
sleep 1
2828
curl -s -o /dev/null http://localhost:8081/owners/4
2929
sleep 1
30+
curl -s -o /dev/null http://localhost:8081/owners/10/pets/10/edit
31+
sleep 1
32+
curl -s -o /dev/null http://localhost:8081/owners?page=0
33+
sleep 1
3034
curl -s -o /dev/null http://localhost:8081/vets.html
3135
sleep 1
36+
curl -s -o /dev/null http://localhost:8081/vets.html?page=0
37+
sleep 1
3238
curl -s -o /dev/null http://localhost:8081/vets
3339
sleep 1
3440
curl -s -o /dev/null http://localhost:8081/vets/Carter

0 commit comments

Comments
 (0)