private <T> T raiseExceptionOnError(final Either<AttachmentError, T> result) throws AttachmentException { return Eithers.getOrThrow(result.left().map(new Function<AttachmentError, AttachmentException>() { @Override public AttachmentException apply(final AttachmentError attachmentError) { return new AttachmentException(String.format("Got error while saving attachment %s: %s", attachmentError.getFilename(), attachmentError.getLogMessage()), attachmentError.getException().getOrNull()); } })); }