From c7b8cc72fc81a5640357b1bd69627b514a73bd2a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 1 May 2024 23:14:08 +0200 Subject: [PATCH 1/2] Display more user-friendly error messages Instead of displaying the Java exception, this change displays a more user-friendly message for some common network-related issues. Fixes mastodon/mastodon-android#667 --- .../android/api/MastodonErrorResponse.java | 23 +++++++++++++++++-- mastodon/src/main/res/values/strings.xml | 5 ++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/MastodonErrorResponse.java b/mastodon/src/main/java/org/joinmastodon/android/api/MastodonErrorResponse.java index 9dfbfdc8..70b21d6d 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/MastodonErrorResponse.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/MastodonErrorResponse.java @@ -5,31 +5,50 @@ import android.view.View; import android.widget.TextView; import android.widget.Toast; +import com.google.gson.JsonIOException; +import com.google.gson.JsonSyntaxException; + import org.joinmastodon.android.R; +import java.net.SocketTimeoutException; +import java.net.UnknownHostException; + import me.grishka.appkit.api.ErrorResponse; public class MastodonErrorResponse extends ErrorResponse{ public final String error; public final int httpStatus; public final Throwable underlyingException; + public final int messageResource; public MastodonErrorResponse(String error, int httpStatus, Throwable exception){ this.error=error; this.httpStatus=httpStatus; this.underlyingException=exception; + + if(exception instanceof UnknownHostException){ + this.messageResource=R.string.could_not_reach_server; + }else if(exception instanceof SocketTimeoutException){ + this.messageResource=R.string.connection_timed_out; + }else if(exception instanceof JsonSyntaxException || exception instanceof JsonIOException || httpStatus>=500){ + this.messageResource=R.string.server_error; + }else if(httpStatus == 404){ + this.messageResource=R.string.not_found; + }else{ + this.messageResource=R.string.unknown_error; + } } @Override public void bindErrorView(View view){ TextView text=view.findViewById(R.id.error_text); - text.setText(error); + text.setText(view.getContext().getString(messageResource, error)); } @Override public void showToast(Context context){ if(context==null) return; - Toast.makeText(context, error, Toast.LENGTH_SHORT).show(); + Toast.makeText(context, context.getString(messageResource, error), Toast.LENGTH_SHORT).show(); } } diff --git a/mastodon/src/main/res/values/strings.xml b/mastodon/src/main/res/values/strings.xml index 069e370f..b36247e1 100644 --- a/mastodon/src/main/res/values/strings.xml +++ b/mastodon/src/main/res/values/strings.xml @@ -755,4 +755,9 @@ View favorites Undo boost Undo favorite + Could not reach the server. Make sure you\'re connected to the internet and try again. + Connection timed out. Make sure you\'re connected to the internet and try again. + There may be a problem with your instance or your internet. Make sure you have access to the internet and try again. + Not found. It probably doesn\'t exist anymore. + Unknown error: %s \ No newline at end of file From 629e65edbad246f4c90dca20e20b9ff4f957bec8 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 13 May 2024 00:08:18 +0200 Subject: [PATCH 2/2] Update error messages and remove the unknown error text --- .../android/api/MastodonErrorResponse.java | 20 +++++++++++++++---- mastodon/src/main/res/values/strings.xml | 9 ++++----- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/MastodonErrorResponse.java b/mastodon/src/main/java/org/joinmastodon/android/api/MastodonErrorResponse.java index 70b21d6d..f7d69066 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/MastodonErrorResponse.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/MastodonErrorResponse.java @@ -32,23 +32,35 @@ public class MastodonErrorResponse extends ErrorResponse{ this.messageResource=R.string.connection_timed_out; }else if(exception instanceof JsonSyntaxException || exception instanceof JsonIOException || httpStatus>=500){ this.messageResource=R.string.server_error; - }else if(httpStatus == 404){ + }else if(httpStatus==404){ this.messageResource=R.string.not_found; }else{ - this.messageResource=R.string.unknown_error; + this.messageResource=0; } } @Override public void bindErrorView(View view){ TextView text=view.findViewById(R.id.error_text); - text.setText(view.getContext().getString(messageResource, error)); + String message; + if(messageResource>0){ + message=view.getContext().getString(messageResource, error); + }else{ + message=error; + } + text.setText(message); } @Override public void showToast(Context context){ if(context==null) return; - Toast.makeText(context, context.getString(messageResource, error), Toast.LENGTH_SHORT).show(); + String message; + if(messageResource>0){ + message=context.getString(messageResource, error); + }else{ + message=error; + } + Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } } diff --git a/mastodon/src/main/res/values/strings.xml b/mastodon/src/main/res/values/strings.xml index b36247e1..5f3886ff 100644 --- a/mastodon/src/main/res/values/strings.xml +++ b/mastodon/src/main/res/values/strings.xml @@ -755,9 +755,8 @@ View favorites Undo boost Undo favorite - Could not reach the server. Make sure you\'re connected to the internet and try again. - Connection timed out. Make sure you\'re connected to the internet and try again. - There may be a problem with your instance or your internet. Make sure you have access to the internet and try again. - Not found. It probably doesn\'t exist anymore. - Unknown error: %s + Couldn’t reach the server. Check your connection and try again? + The request timed out. Check your connection and try again? + Something went wrong talking with your server. It’s probably not your fault. Try again? + It could’ve been deleted, or maybe it never existed at all. \ No newline at end of file