Add handleException() to DbViewModel

and use it for blogs
This commit is contained in:
Torsten Grote
2021-03-22 15:17:30 -03:00
parent e97478a21a
commit 4074ac8578
4 changed files with 31 additions and 8 deletions

View File

@@ -142,7 +142,7 @@ abstract class BaseViewModel extends DbViewModel implements EventListener {
runOnDbThread(true, txn -> {
BlogPostItem item = getItem(txn, header);
txn.attach(() -> onBlogPostItemAdded(item, local));
}, e -> logException(LOG, WARNING, e));
}, this::handleException);
}
@UiThread
@@ -163,7 +163,7 @@ abstract class BaseViewModel extends DbViewModel implements EventListener {
BlogPostHeader h = item.getHeader();
blogManager.addLocalComment(a, b.getId(), comment, h);
} catch (DbException e) {
logException(LOG, WARNING, e);
handleException(e);
}
});
}

View File

@@ -39,10 +39,8 @@ import androidx.annotation.UiThread;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.util.LogUtils.logDuration;
import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.LogUtils.now;
@MethodsNotNullByDefault
@@ -132,7 +130,7 @@ class BlogViewModel extends BaseViewModel {
blog.postValue(new BlogItem(b, ours, removable));
logDuration(LOG, "Loading blog", start);
} catch (DbException e) {
logException(LOG, WARNING, e);
handleException(e);
}
});
}
@@ -155,7 +153,7 @@ class BlogViewModel extends BaseViewModel {
Collection<Contact> contacts =
blogSharingManager.getSharedWith(txn, groupId);
txn.attach(() -> onSharingContactsLoaded(contacts));
}, e -> logException(LOG, WARNING, e));
}, this::handleException);
}
@UiThread
@@ -173,7 +171,7 @@ class BlogViewModel extends BaseViewModel {
blogManager.removeBlog(b);
logDuration(LOG, "Removing blog", start);
} catch (DbException e) {
logException(LOG, WARNING, e);
handleException(e);
}
});
}

View File

@@ -105,7 +105,7 @@ class FeedViewModel extends BaseViewModel {
logDuration(LOG, "Loading personal blog", start);
personalBlog.postValue(b);
} catch (DbException e) {
logException(LOG, WARNING, e);
handleException(e);
}
});
}

View File

@@ -1,6 +1,7 @@
package org.briarproject.briar.android.viewmodel;
import android.app.Application;
import android.widget.Toast;
import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbCallable;
@@ -11,6 +12,7 @@ import org.briarproject.bramble.api.db.TransactionManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.system.AndroidExecutor;
import org.briarproject.bramble.util.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
@@ -21,6 +23,7 @@ import java.util.logging.Logger;
import javax.annotation.concurrent.Immutable;
import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
@@ -31,6 +34,7 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;
import static android.widget.Toast.LENGTH_LONG;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.util.LogUtils.logException;
@@ -276,4 +280,25 @@ public abstract class DbViewModel extends AndroidViewModel {
return new ArrayList<>(list);
}
/**
* Logs the exception and shows a Toast to the user.
* <p>
* Errors that are likely or expected to happen should not use this method
* and show proper error states in UI.
*/
@AnyThread
protected void handleException(Exception e) {
logException(LOG, WARNING, e);
androidExecutor.runOnUiThread(() -> {
String msg = "Error: " + e.getClass().getSimpleName();
if (!StringUtils.isNullOrEmpty(e.getMessage())) {
msg += " " + e.getMessage();
}
if (e.getCause() != null) {
msg += " caused by " + e.getCause().getClass().getSimpleName();
}
Toast.makeText(getApplication(), msg, LENGTH_LONG).show();
});
}
}