https://kotlinlang.org logo
#codereview
Title
# codereview
a

aoriani

12/31/2019, 12:36 AM
If that "error handling strategy" is okay for you.
c

csturtevant

12/31/2019, 5:29 AM
I am using OkHTTP, the
response
is a
Copy code
org.springframework.http.ResponseEntity<List<Task>>
with
Task
being a regular POJO.
val tasks = response.body ?: arrayListOf<Task>()
does look better but if you have any other suggestions based on the new evidence I’d be highly appreciative!
a

aoriani

12/31/2019, 9:00 AM
Okay, so it is actually https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/HttpEntity.html#getBody-- which is also annotated with
@Nullable
so Kotlin can at least say it returns a nullable type. Thus coercing to a non-null could be a problem. Now the best code actually depends of the context. I am not familiar with Spring.
I imagine that
getBody()
is nullable because
ResponseEntity
represents any HTTP response and not all responses have a body. An HTTP 204, for instance, doesn't have a body.
In your case what would missing body mean? That there're no `Task`s ? That there is an error ? You are not checking whether the request actually succeeds (if at least the response has a 200 status code). So assuming it will always succeed is in itself a problem. In both cases, would it be okay to return an empty list ? Or it is better to throw an exception to know straight away that something went wrong?
👍 1
2 Views