Catch unexpected exceptions in MessagePreviewCreator

This commit is contained in:
cketti 2020-10-06 15:14:59 +02:00
parent 15ca949698
commit e3b8855bdd
2 changed files with 19 additions and 0 deletions

View file

@ -6,6 +6,8 @@ import androidx.annotation.NonNull;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.Part;
import timber.log.Timber;
public class MessagePreviewCreator {
private final TextPartFinder textPartFinder;
@ -33,6 +35,10 @@ public class MessagePreviewCreator {
String previewText = previewTextExtractor.extractPreview(textPart);
return PreviewResult.text(previewText);
} catch (PreviewExtractionException e) {
Timber.w(e, "Failed to extract preview text");
return PreviewResult.error();
} catch (Exception e) {
Timber.e(e, "Unexpected error while trying to extract preview text");
return PreviewResult.error();
}
}

View file

@ -83,6 +83,19 @@ public class MessagePreviewCreatorTest {
assertEquals(PreviewType.ERROR, result.getPreviewType());
}
@Test
public void createPreview_withPreviewTextExtractorThrowingUnexpectedException() throws Exception {
Message message = createDummyMessage();
Part textPart = createTextPart("text/plain");
when(textPartFinder.findFirstTextPart(message)).thenReturn(textPart);
when(previewTextExtractor.extractPreview(textPart)).thenThrow(new IllegalStateException(""));
PreviewResult result = previewCreator.createPreview(message);
assertFalse(result.isPreviewTextAvailable());
assertEquals(PreviewType.ERROR, result.getPreviewType());
}
private Message createDummyMessage() {
return new MimeMessage();
}