Skip to content

Crash when server is offline and returning non-pixelfed error page

When the server is offline and behind cloudflare for example, the app will get back an error but won't be able to interpret it as an Error. This crashes with a JsonSyntaxException:

Stacktrace: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
......
at com.google.gson.Gson.fromJson(Gson.java:813)
at org.pixeldroid.app.posts.feeds.CommonFeedFragmentUtilsKt$initAdapter$3.invoke(CommonFeedFragmentUtils.kt:86)

See code here: fromJson throws JsonSyntaxException

https://gitlab.shinice.net/pixeldroid/PixelDroid/-/blob/e3c0be4dbff34c36a5cafa2143e34e1a2eba9d1c/app/src/main/java/org/pixeldroid/app/posts/feeds/CommonFeedFragmentUtils.kt#L85-87

To fix:

  • Try/catch the error (catch Exception superclass in case anything else goes wrong)
  • If exception caught: try HTTP error code, else print generic error?
Edited by Matthieu