Skip to content

Commit 6e63300

Browse files
committed
🥅 Feature handle 404 errors
1 parent 005a7d0 commit 6e63300

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/main/kotlin/com/rsginer/hackathoon/banks/controller/BankController.kt

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ package com.rsginer.hackathoon.banks.controller
22

33
import com.rsginer.hackathoon.banks.model.Bank
44
import com.rsginer.hackathoon.banks.service.BankService
5-
import org.springframework.web.bind.annotation.GetMapping
6-
import org.springframework.web.bind.annotation.PathVariable
7-
import org.springframework.web.bind.annotation.RequestMapping
8-
import org.springframework.web.bind.annotation.RestController
5+
import org.springframework.http.HttpStatus
6+
import org.springframework.http.ResponseEntity
7+
import org.springframework.web.bind.annotation.*
98

109
@RestController
1110
@RequestMapping("api/banks")
1211
class BankController(
1312
private val bankService: BankService
1413
) {
1514

15+
@ExceptionHandler(NoSuchElementException::class)
16+
fun handleNotFound(e: NoSuchElementException): ResponseEntity<String> =
17+
ResponseEntity(e.message, HttpStatus.NOT_FOUND)
18+
1619
@GetMapping
1720
fun getBanks(): Collection<Bank> = this.bankService.getBanks()
1821

1922
@GetMapping("/{accountNumber}")
20-
fun getBankByAccountNumber(@PathVariable accountNumber: String): Bank {
21-
return this.bankService.getBank(accountNumber)
22-
}
23+
fun getBankByAccountNumber(@PathVariable accountNumber: String): Bank = this.bankService.getBank(accountNumber)
24+
2325
}

src/main/kotlin/com/rsginer/hackathoon/banks/datasource/mock/MockBankDataSource.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ class MockBankDataSource : BankDataSource {
1414
)
1515

1616
override fun retrieveBanks(): Collection<Bank> = banks
17-
override fun retrieveBank(accountNumber: String): Bank = banks.first { it.accountNumber == accountNumber }
17+
override fun retrieveBank(accountNumber: String): Bank = banks.firstOrNull() { it.accountNumber == accountNumber }
18+
?: throw NoSuchElementException("Could not find a bank with accountNumber $accountNumber")
1819
}

src/test/kotlin/com/rsginer/hackathoon/banks/controller/BankControllerTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ internal class BankControllerTest {
7171
.andDo { print() }
7272
.andExpect {
7373
status { isNotFound() }
74-
content { contentType(MediaType.APPLICATION_JSON) }
7574
}
7675
}
7776
}

0 commit comments

Comments
 (0)