Lottie-android: Unable to catch exception due to parsing errors in json

Created on 14 Nov 2018  ·  7Comments  ·  Source: airbnb/lottie-android

Use Case:
My use case revolves around allowing users to upload animation file. However since on the server side user can provide a file that might not have correct animation format. I need to ensure in that case the app doesn't crashes or library throws appropriate exception that can be caught.
Currently app crashes on trying to parse the corrupted/invalid json.
Is there any way to catch this exception and show useful message on the client side ?

Version: 2.7.0
Code

try {
//playing animation from model url
tv_animation.setAnimationFromUrl(model?.celebration_url)
} catch (e: Exception){
//playing local backup animation
showKonfetti()
}

Error: Unable to catch parse exception.

stack trace:

java.lang.IllegalStateException: Unable to parse composition
at com.airbnb.lottie.LottieAnimationView$2.onResult(LottieAnimationView.java:68)
at com.airbnb.lottie.LottieAnimationView$2.onResult(LottieAnimationView.java:66)
at com.airbnb.lottie.LottieTask.notifyFailureListeners(LottieTask.java:167)
at com.airbnb.lottie.LottieTask.access$300(LottieTask.java:26)
at com.airbnb.lottie.LottieTask$1.run(LottieTask.java:142)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: android.util.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 20159
at android.util.JsonReader.syntaxError(JsonReader.java:1159)
at android.util.JsonReader.checkLenient(JsonReader.java:838)
at android.util.JsonReader.nextInObject(JsonReader.java:660)
at android.util.JsonReader.peek(JsonReader.java:349)
at android.util.JsonReader.hasNext(JsonReader.java:319)
at com.airbnb.lottie.parser.ShapeGroupParser.parse(ShapeGroupParser.java:22)
at com.airbnb.lottie.parser.ContentModelParser.parse(ContentModelParser.java:48)
at com.airbnb.lottie.parser.LayerParser.parse(LayerParser.java:113)
at com.airbnb.lottie.parser.LottieCompositionParser.parseLayers(LottieCompositionParser.java:104)
at com.airbnb.lottie.parser.LottieCompositionParser.parse(LottieCompositionParser.java:72)
at com.airbnb.lottie.LottieCompositionFactory.fromJsonReaderSync(LottieCompositionFactory.java:229)
at com.airbnb.lottie.LottieCompositionFactory.fromJsonInputStreamSync(LottieCompositionFactory.java:163)
at com.airbnb.lottie.LottieCompositionFactory.fromJsonInputStreamSync(LottieCompositionFactory.java:157)
at com.airbnb.lottie.network.NetworkFetcher.fetchFromNetworkInternal(NetworkFetcher.java:133)
at com.airbnb.lottie.network.NetworkFetcher.fetchFromNetwork(NetworkFetcher.java:93)
at com.airbnb.lottie.network.NetworkFetcher.fetchSync(NetworkFetcher.java:62)
at com.airbnb.lottie.network.NetworkFetcher$1.call(NetworkFetcher.java:49)
at com.airbnb.lottie.network.NetworkFetcher$1.call(NetworkFetcher.java:47)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)

Most helpful comment

@Yahyaali1 @xiaobinghe If you are concerned with animations failing to parse, use the LottieCompositionFactory APIs and manually set the resulting composition on your LottieAnimationView.

You can add a failure listener on them.

All 7 comments

The same case appeared in my project , and the exception cannot be caught;

Specially if class can prevent this crash by handling it in catch block or allowing to access the private listener throwable so that we can attach listener as per our need.

private final LottieListener failureListener = new LottieListener() {
@Override public void onResult(Throwable throwable) {
throw new IllegalStateException("Unable to parse composition", throwable);
}
};

The same case appeared in my project , and the exception cannot be caught;

@xiaobinghe any approach towards resolving this issue ?

@Yahyaali1 @xiaobinghe If you are concerned with animations failing to parse, use the LottieCompositionFactory APIs and manually set the resulting composition on your LottieAnimationView.

You can add a failure listener on them.

@Yahyaali1 @xiaobinghe If you are concerned with animations failing to parse, use the LottieCompositionFactory APIs and manually set the resulting composition on your LottieAnimationView.

You can add a failure listener on them.

thank you!
This method is perfect

The same case appeared in my project , and the exception cannot be caught;

@xiaobinghe any approach towards resolving this issue ?
the method imageView.setAnimationFromUrl() is called in other thread;so we could not catch exceptions; The solution of @gpeal provided is good ;

LottieTask<LottieComposition> lottieCompositionLottieTask = LottieCompositionFactory.fromUrl(context, titleUrl); lottieCompositionLottieTask.addListener(new LottieListener<LottieComposition>() { @Override public void onResult(LottieComposition result) { imageView.setComposition(result); imageView.setRepeatCount(isRepeat ? ValueAnimator.INFINITE : 0); imageView.playAnimation(); } });

@Yahyaali1 @xiaobinghe If you are concerned with animations failing to parse, use the LottieCompositionFactory APIs and manually set the resulting composition on your LottieAnimationView.

You can add a failure listener on them.

What about iOS?

Was this page helpful?
0 / 5 - 0 ratings