Wrap throwables

pull/214/head
M66B 2 years ago
parent 37e145a9d6
commit c4a1384232

@ -155,7 +155,7 @@ public class ActivityDMARC extends ActivityBase {
if (ex instanceof NoStreamException) if (ex instanceof NoStreamException)
((NoStreamException) ex).report(ActivityDMARC.this); ((NoStreamException) ex).report(ActivityDMARC.this);
else else
tvDmarc.setText(ex + "\n" + android.util.Log.getStackTraceString(ex)); Log.unexpectedError(getSupportFragmentManager(), ex);
grpReady.setVisibility(View.VISIBLE); grpReady.setVisibility(View.VISIBLE);
} }
}.execute(this, args, "dmarc:decode"); }.execute(this, args, "dmarc:decode");
@ -583,7 +583,7 @@ public class ActivityDMARC extends ActivityBase {
ssb.append(' '); ssb.append(' ');
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
ssb.append(ex.toString()).append('\n'); ssb.append(new ThrowableWrapper(ex).toSafeString()).append('\n');
} }
try { try {
@ -592,7 +592,7 @@ public class ActivityDMARC extends ActivityBase {
ssb.append('(').append(info.org).append(") "); ssb.append('(').append(info.org).append(") ");
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
ssb.append(ex.toString()).append('\n'); ssb.append(new ThrowableWrapper(ex).toSafeString()).append('\n');
} }
} }
@ -612,7 +612,7 @@ public class ActivityDMARC extends ActivityBase {
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
ssb.append(ex.toString()).append('\n'); ssb.append(new ThrowableWrapper(ex).toSafeString()).append('\n');
} }
return result; return result;

@ -189,7 +189,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
tvCondition.setText(ssb); tvCondition.setText(ssb);
} catch (Throwable ex) { } catch (Throwable ex) {
tvCondition.setText(ex.getMessage()); tvCondition.setText(new ThrowableWrapper(ex).getSafeMessage());
} }
try { try {
@ -279,7 +279,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
}.execute(context, owner, args, "rule:folder"); }.execute(context, owner, args, "rule:folder");
} }
} catch (Throwable ex) { } catch (Throwable ex) {
tvAction.setText(ex.getMessage()); tvAction.setText(new ThrowableWrapper(ex).getSafeMessage());
} }
tvLastApplied.setText(rule.last_applied == null ? "-" : DF.format(rule.last_applied)); tvLastApplied.setText(rule.last_applied == null ? "-" : DF.format(rule.last_applied));

@ -7074,7 +7074,7 @@ public class FragmentCompose extends FragmentBase {
R.string.title_address_duplicate, R.string.title_address_duplicate,
TextUtils.join(", ", dup))); TextUtils.join(", ", dup)));
} catch (AddressException ex) { } catch (AddressException ex) {
args.putString("address_error", ex.getMessage()); args.putString("address_error", new ThrowableWrapper(ex).getSafeMessage());
} }
if (draft.to == null && draft.cc == null && draft.bcc == null && if (draft.to == null && draft.cc == null && draft.bcc == null &&
@ -7479,7 +7479,7 @@ public class FragmentCompose extends FragmentBase {
address.validate(); address.validate();
} catch (AddressException ex) { } catch (AddressException ex) {
throw new AddressException(context.getString(R.string.title_address_parse_error, throw new AddressException(context.getString(R.string.title_address_parse_error,
MessageHelper.formatAddressesCompose(new Address[]{address}), ex.getMessage())); MessageHelper.formatAddressesCompose(new Address[]{address}), new ThrowableWrapper(ex).getSafeMessage()));
} }
} }

@ -426,7 +426,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
tvHost.setText(ex.getClass().getName()); tvHost.setText(ex.getClass().getName());
tvOwner.setText(ex.getMessage()); tvOwner.setText(new ThrowableWrapper(ex).getSafeMessage());
} }
}.execute(FragmentDialogOpenLink.this, args, "link:owner"); }.execute(FragmentDialogOpenLink.this, args, "link:owner");
} }

@ -197,7 +197,7 @@ public class FragmentDialogRuleCheck extends FragmentDialogBase {
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException) { if (ex instanceof IllegalArgumentException) {
tvNoMessages.setText(ex.getMessage()); tvNoMessages.setText(new ThrowableWrapper(ex).getSafeMessage());
tvNoMessages.setVisibility(View.VISIBLE); tvNoMessages.setVisibility(View.VISIBLE);
} else } else
Log.unexpectedError(getParentFragmentManager(), ex); Log.unexpectedError(getParentFragmentManager(), ex);

@ -205,7 +205,7 @@ public class FragmentGmail extends FragmentBase {
startActivityForResult(intent, ActivitySetup.REQUEST_CHOOSE_ACCOUNT); startActivityForResult(intent, ActivitySetup.REQUEST_CHOOSE_ACCOUNT);
} catch (Throwable ex) { } catch (Throwable ex) {
if (ex instanceof IllegalArgumentException) if (ex instanceof IllegalArgumentException)
tvError.setText(ex.getMessage()); tvError.setText(new ThrowableWrapper(ex).getSafeMessage());
else else
tvError.setText(Log.formatThrowable(ex, false)); tvError.setText(Log.formatThrowable(ex, false));
grpError.setVisibility(View.VISIBLE); grpError.setVisibility(View.VISIBLE);
@ -631,7 +631,7 @@ public class FragmentGmail extends FragmentBase {
Log.e(ex); Log.e(ex);
if (ex instanceof IllegalArgumentException) if (ex instanceof IllegalArgumentException)
tvError.setText(ex.getMessage()); tvError.setText(new ThrowableWrapper(ex).getSafeMessage());
else else
tvError.setText(Log.formatThrowable(ex, false)); tvError.setText(Log.formatThrowable(ex, false));
grpError.setVisibility(View.VISIBLE); grpError.setVisibility(View.VISIBLE);

@ -1092,7 +1092,7 @@ public class FragmentOAuth extends FragmentBase {
return; return;
if (ex instanceof IllegalArgumentException) if (ex instanceof IllegalArgumentException)
tvError.setText(ex.getMessage()); tvError.setText(new ThrowableWrapper(ex).getSafeMessage());
else else
tvError.setText(Log.formatThrowable(ex, false)); tvError.setText(Log.formatThrowable(ex, false));

@ -737,7 +737,7 @@ public class FragmentQuickSetup extends FragmentBase {
grpManual.setVisibility(View.VISIBLE); grpManual.setVisibility(View.VISIBLE);
if (ex instanceof IllegalArgumentException || ex instanceof UnknownHostException) { if (ex instanceof IllegalArgumentException || ex instanceof UnknownHostException) {
tvError.setText(ex.getMessage()); tvError.setText(new ThrowableWrapper(ex).getSafeMessage());
grpError.setVisibility(View.VISIBLE); grpError.setVisibility(View.VISIBLE);
getMainHandler().post(new Runnable() { getMainHandler().post(new Runnable() {

@ -1760,7 +1760,7 @@ public class Helper {
tvType.setText(type); tvType.setText(type);
tvException.setText(ex == null ? null : ex.toString()); tvException.setText(ex == null ? null : new ThrowableWrapper(ex).toSafeString());
tvException.setVisibility(ex == null ? View.GONE : View.VISIBLE); tvException.setVisibility(ex == null ? View.GONE : View.VISIBLE);
AlertDialog.Builder builder = new AlertDialog.Builder(context) AlertDialog.Builder builder = new AlertDialog.Builder(context)

@ -246,7 +246,7 @@ public class Log {
int def = (BuildConfig.DEBUG ? android.util.Log.INFO : android.util.Log.WARN); int def = (BuildConfig.DEBUG ? android.util.Log.INFO : android.util.Log.WARN);
level = prefs.getInt("log_level", def); level = prefs.getInt("log_level", def);
} }
android.util.Log.d(TAG, "Log level=" + level); Log.i("Log level=" + level);
} }
public static boolean isDebugLogLevel() { public static boolean isDebugLogLevel() {
@ -288,7 +288,7 @@ public class Log {
public static int e(String msg) { public static int e(String msg) {
if (BuildConfig.BETA_RELEASE) if (BuildConfig.BETA_RELEASE)
try { try {
Throwable ex = new Throwable(msg); Throwable ex = new ThrowableWrapper(msg);
List<StackTraceElement> ss = new ArrayList<>(Arrays.asList(ex.getStackTrace())); List<StackTraceElement> ss = new ArrayList<>(Arrays.asList(ex.getStackTrace()));
ss.remove(0); ss.remove(0);
ex.setStackTrace(ss.toArray(new StackTraceElement[0])); ex.setStackTrace(ss.toArray(new StackTraceElement[0]));
@ -1816,16 +1816,16 @@ public class Log {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (BuildConfig.DEBUG) if (BuildConfig.DEBUG)
sb.append(ex.toString()); sb.append(new ThrowableWrapper(ex).toSafeString());
else else
sb.append(ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage()); sb.append(new ThrowableWrapper(ex).getSafeMessageOrName());
Throwable cause = ex.getCause(); Throwable cause = ex.getCause();
while (cause != null) { while (cause != null) {
if (BuildConfig.DEBUG) if (BuildConfig.DEBUG)
sb.append(separator).append(cause.toString()); sb.append(separator).append(new ThrowableWrapper(cause).toSafeString());
else else
sb.append(separator).append(cause.getMessage() == null ? cause.getClass().getName() : cause.getMessage()); sb.append(separator).append(new ThrowableWrapper(cause).getSafeMessageOrName());
cause = cause.getCause(); cause = cause.getCause();
} }
@ -1838,7 +1838,7 @@ public class Log {
try (FileWriter out = new FileWriter(file, true)) { try (FileWriter out = new FileWriter(file, true)) {
out.write(BuildConfig.VERSION_NAME + BuildConfig.REVISION + " " + new Date() + "\r\n"); out.write(BuildConfig.VERSION_NAME + BuildConfig.REVISION + " " + new Date() + "\r\n");
out.write(ex + "\r\n" + android.util.Log.getStackTraceString(ex) + "\r\n"); out.write(ex + "\r\n" + new ThrowableWrapper(ex).getStackTraceString() + "\r\n");
} catch (IOException e) { } catch (IOException e) {
Log.e(e); Log.e(e);
} }
@ -3783,7 +3783,7 @@ public class Log {
} }
ssb.append("\r\n"); ssb.append("\r\n");
} catch (Throwable ex) { } catch (Throwable ex) {
ssb.append(ex.toString()); ssb.append(new ThrowableWrapper(ex).toSafeString());
} }
ssb.setSpan(new RelativeSizeSpan(HtmlHelper.FONT_SMALL), 0, ssb.length(), 0); ssb.setSpan(new RelativeSizeSpan(HtmlHelper.FONT_SMALL), 0, ssb.length(), 0);

@ -5567,12 +5567,12 @@ public class MessageHelper {
getStructure(multipart.getBodyPart(i), ssb, level + 1, textColorLink); getStructure(multipart.getBodyPart(i), ssb, level + 1, textColorLink);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
ssb.append(ex.toString()).append('\n'); ssb.append(new ThrowableWrapper(ex).toSafeString()).append('\n');
} }
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
ssb.append(ex.toString()).append('\n'); ssb.append(new ThrowableWrapper(ex).toSafeString()).append('\n');
} }
} }

@ -292,7 +292,7 @@ public class ProtectedContent {
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
tvError.setText(ex.getMessage()); tvError.setText(new ThrowableWrapper(ex).getSafeMessage());
tvErrorDetail.setText(ex.toString()); tvErrorDetail.setText(ex.toString());
tvError.setVisibility(View.VISIBLE); tvError.setVisibility(View.VISIBLE);
tvErrorDetail.setVisibility(View.VISIBLE); tvErrorDetail.setVisibility(View.VISIBLE);

@ -0,0 +1,61 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2023 by Marcel Bokhorst (M66B)
*/
import android.text.TextUtils;
import androidx.annotation.Nullable;
public class ThrowableWrapper extends Throwable {
private Throwable ex;
private String msg;
ThrowableWrapper(String msg) {
this.ex = new Throwable();
this.msg = msg;
}
ThrowableWrapper(Throwable ex) {
this.ex = ex;
}
@Nullable
@Override
public String getLocalizedMessage() {
return getSafeMessage();
}
public String getSafeMessage() {
return (TextUtils.isEmpty(msg) ? super.getMessage() : msg);
}
public String getSafeMessageOrName() {
String msg = getSafeMessage();
return (msg == null ? ex.getClass().getName() : msg);
}
public String getStackTraceString() {
return android.util.Log.getStackTraceString(ex);
}
public String toSafeString() {
return super.toString();
}
}
Loading…
Cancel
Save