Iliyan Germanov
04/07/2023, 9:58 PMcatch({}){}
. Are there docs on that? Code in the 🧵
• The KDoc is minimal and lacks examples. For example, on raise()
we have just "Raise a logical failure of type Error". Btw GPT-4 generate killer docs if you give him some context and good prompts - ofc it needs human verification but it can greatly speed-up the docs process.
• Other than that - you've done an excellent job! I really enjoy to upgrade! 👏Iliyan Germanov
04/07/2023, 9:59 PMIliyan Germanov
04/07/2023, 10:00 PMerror()
context(HttpClient)
private suspend fun fetchRatesFrom(url: String): Either<String, FawazahmedResponse> = catch({
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val response = get(url)
if (!response.status.isSuccess()) {
error("Unsuccessful response code - ${response.status.value}: ${response.body<String>()}")
}
Right(response.body())
}
}) { e: Exception ->
Left(e.message ?: "Unknown error")
}
Iliyan Germanov
04/07/2023, 10:02 PMsimon.vergauwen
04/08/2023, 8:49 AMcontext(HttpClient)
private suspend fun fetchRatesFrom(url: String): Either<String, FawazahmedResponse> = catch({
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val response = get(url)
if (!response.status.isSuccess()) {
error("Unsuccessful response code - ${response.status.value}: ${response.body<String>()}")
}
Right(response.body())
}
}) { e: Exception ->
Left(e.message ?: "Unknown error")
}
You probably just want either
with nested `catch`around get
. If you don't mind get
throwing unexpected failures, you can also omit the catch
. Some more information: https://arrow-kt.io/learn/typed-errors/working-with-typed-errors/#from-exceptions
context(HttpClient)
private suspend fun fetchRatesFrom(url: String): Either<String, FawazahmedResponse> =
either {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val response = catch({ get(url) }) { e: Exception ->
Left(e.message ?: "Unknown error")
}
ensure(response.status.isSuccess()) {
"Unsuccessful response code - ${response.status.value}: ${response.body<String>()}"
}
response.body()
}
simon.vergauwen
04/08/2023, 8:50 AMIliyan Germanov
04/08/2023, 9:02 AMeither
and catch
- awesome. Thank you for the refactoring, I like it!
Regarding the docs, as a lazy dev who uses only one screen, I'd like to have everything I need in IDEA. I want to be able to press "CTRL + Q" and see short explanation + examples on every method, type everything. I know that this is overkill for you but hey we the community can help.
IMO, the benefit of doing this is that it'll help newcomers to FP and Arrow greatly. For example the thing you shown me above may deserve a place in the either
or catch
simon.vergauwen
04/08/2023, 9:04 AMIliyan Germanov
04/10/2023, 1:35 PMRaise
.