question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Retrofit issue with Coroutine

See original GitHub issue

hi

i use retrofit 2.6.0, with kotlin coroutines 1.2.1,

  1. so when use Flow an exception appear:
@GET("/") suspend fun fetch(): Flow<User>

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `kotlinx.coroutines.flow.Flow`
  1. i have this case
GET("/{id}") suspend fun loadBy(@Path("id") id: Int): User? 

if suppose that the back-end return NotFoundException(…) in failure, so the front-end (client) have an exception of can’t cast NotFoundException to User instance (font-end suppose receive a null instance of User in failure) so its possible to use Result<User> instead of User? nullable type however when try to use it i see kotlin not allow Result as return type

  1. even if try to use Result<User> instead of User?, an other exception appear:
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `kotlin.Result` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
halimpuckjavacommented, Jul 8, 2019

backend-side (developed with NESTJS)

@Entity()
export class User {
    @PrimaryGeneratedColumn('increment') id: number
    @Column({type: 'varchar', length: 20}) name: string
    @CreateDateColumn() createAt?: Date
}

export class UserController {
  constructor(private readonly userRepository: Repository<User>) {}

  @Get(':id')
  findById(@Param('id') id: number): Observable<any> {
     return from(this.userRepository.findOneOrFail(id)).pipe(catchError(err => of(new NotFoundException(`user [id:${id}] not found`))))
  
}

in front-end (retrofit client):

@GET("/{id}") suspend fun findById(@Path("id") id: Int): User?

so when client send request to server to find user by id it expect to return user or null if not found, but in server side when user not found, server return exception of [NotFoundException] as response, so when client receive response (NotFoundException) it can’t cast it to User nullable instance and throw an exception of casting error

0reactions
JakeWhartoncommented, Nov 19, 2019

You would have to build it yourself. Retrofit only supports suspend functions for coroutines, not Flow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin Coroutines and Retrofit - Medium
Okay so Retrofit is a type-safe HTTP client for Android and Java. ... which uses Kotlin coroutine's Deferred type if you're using a...
Read more >
Retrofit with Kotlin Coroutine in Android - GeeksforGeeks
Retrofit is a type-safe http client which is used to retrieve, update and delete the data from web services. Nowadays retrofit library is ......
Read more >
Retrofit — Effective error handling with Kotlin Coroutine and ...
When the OkHttp client receives a response from the server, it passes the response back to the Retrofit. Retrofit then pushes the meaningless...
Read more >
Suspend what you're doing: Retrofit has now Coroutines ...
It official now! Retrofit 2.6.0 has been released with support for suspend functions. This allows you to express the asynchrony of HTTP requests ......
Read more >
Coroutines, Retrofit, and a nice way to handle Responses
An asynchronous programming pattern implemented with coroutines to perform “one-shot calls”. Regarding Android App Architecture our Service and Repository ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found