Jason5lee
03/15/2022, 8:26 AMclass SomethingNotFoundException : Exception()
// DB query side
if (result.isEmpty()) {
throw SomethingNotFoundException()
}
// API implement
try {
....
} catch (e: SomethingNotFoundException) {
return HttpResponse(404, "SOMETHING_NOT_FOUND")
}
2.
// DB query side
if (result.isEmpty()) {
throw HttpException(404, "SOME_THING_NOT_FOUND")
}
where HttpException
is handled by the framework or a single catch
that extracts the response parameter from the exception fields.
The second approach is simpler, but it couples with the protocol as you involves the HTTP exception and related concept like status code.
Which one would you choose?Sam
03/15/2022, 8:39 AMRob Elliot
03/15/2022, 8:50 AMJason5lee
03/15/2022, 8:57 AMthanksforallthefish
03/15/2022, 9:04 AM@ControllerAdvice
and catch all exceptions that extend a generic NotFoundException
and transform those into 404Sam
03/15/2022, 9:12 AMthanksforallthefish
03/15/2022, 9:16 AMJason5lee
03/15/2022, 9:46 AMNotFoundException
, you are coupling with the framework or protocol. I know it may be a widely used practice but I'm just asking.thanksforallthefish
03/15/2022, 9:49 AMRob Elliot
03/15/2022, 9:50 AMNotFoundException
with no HTTP or framework specific coupling, and use your framework's capabilities to map that exception to an HTTP response. Then the coupling is between the framework's HTTP layer & your domain, rather than coupling the data access to HTTP and/or the framework.christophsturm
03/15/2022, 10:14 AMTies
03/15/2022, 10:25 AMchristophsturm
03/15/2022, 10:56 AMTies
03/15/2022, 11:07 AMchristophsturm
03/15/2022, 11:38 AMuser = store.get(key)?: fallbackStore.get(key)