skydoves / sandwich

🥪 Sandwich is an adaptable and lightweight sealed API library designed for handling API responses and exceptions in Kotlin for Retrofit, Ktor, and Kotlin Multiplatform.

Home Page:https://skydoves.github.io/sandwich/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

crash on 401 unauthorized status

iceberg1369 opened this issue · comments

Please complete the following information:

  • Library Version [e.g. v1.0.0]
  • Affected Device(s) [e.g. Samsung Galaxy s10 with Android 9.0]

Describe the Bug:
when I send A user pass to my api which unauthorized and throws 401 status code. the apiresponse call adapter factory crashed.

--------- beginning of crash

2020-09-12 14:16:55.564 7771-7771/com.skydoves.pokedex E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.skydoves.pokedex, PID: 7771
retrofit2.HttpException: HTTP 401 Response.error()
at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.kt:53)
at com.skydoves.sandwich.coroutines.ApiResponseCallDelegate$enqueueImpl$1.onResponse(ApiResponseCallDelegate.kt:33)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:161)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:504)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Add a clear description about the problem.

Expected Behavior:

A clear description of what you expected to happen.

onError expected to be fired

@iceberg1369
Hi, could you let me know about your details of the test codes?

@skydoves

` @provides
@singleton
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.client(okHttpClient)
//.baseUrl("https://pokeapi.co/api/v2/")
.baseUrl("http://37.152.179.135:8082/")
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(CoroutinesResponseCallAdapterFactory())
.build()
}

@FormUrlEncoded
@post("/api/session")
suspend fun addSession(@field("email") email: String?, @field("password") password: String?): ApiResponse<User?>?

@entity
@parcelize
@JsonClass(generateAdapter = true)
data class User(
@field:Json(name = "disabled") val disabled: Boolean?,
@field:Json(name = "email") val email: String?

) : Parcelable

everything works if I submit vali email and password and model data is emitted. but in case of invalid email and password the 401 status code the app crashes.

@skydoves is there anyway to handle errors in sandwitch?

I face the same issue,crash on [ApiResponseCallDelegate.kt:33] when HTTP 400 rerurns

I am facing the same issue : 401 returned instead of onError fired. Commenting to keep tabs on the issue

I will check this issue and release the next version as soon as possible. Thanks!

commented

Hope to have a good solution

Same thing happening for 500 errors , basically for what isn't in that success codes array in the initializer

Hi, @iceberg1369 @MasterXing @bogdanmunteanu @nevermore1000

Yesterday, released a new version 1.0.5 ! 🎉
Please try to test using the new version.

Thanks for reporting this issue! 👍

@skydoves , all working great for 401 and all 504 status codes (on error is triggered) . Will test more and come later with more feedback but all seems good for know. Thanks for the fast release 👍

@skydoves 401 error handled by onError