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
This commit is contained in:
parent
0e868f0be0
commit
c7b8cc72fc
|
@ -5,31 +5,50 @@ import android.view.View;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.google.gson.JsonIOException;
|
||||||
|
import com.google.gson.JsonSyntaxException;
|
||||||
|
|
||||||
import org.joinmastodon.android.R;
|
import org.joinmastodon.android.R;
|
||||||
|
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
import me.grishka.appkit.api.ErrorResponse;
|
import me.grishka.appkit.api.ErrorResponse;
|
||||||
|
|
||||||
public class MastodonErrorResponse extends ErrorResponse{
|
public class MastodonErrorResponse extends ErrorResponse{
|
||||||
public final String error;
|
public final String error;
|
||||||
public final int httpStatus;
|
public final int httpStatus;
|
||||||
public final Throwable underlyingException;
|
public final Throwable underlyingException;
|
||||||
|
public final int messageResource;
|
||||||
|
|
||||||
public MastodonErrorResponse(String error, int httpStatus, Throwable exception){
|
public MastodonErrorResponse(String error, int httpStatus, Throwable exception){
|
||||||
this.error=error;
|
this.error=error;
|
||||||
this.httpStatus=httpStatus;
|
this.httpStatus=httpStatus;
|
||||||
this.underlyingException=exception;
|
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
|
@Override
|
||||||
public void bindErrorView(View view){
|
public void bindErrorView(View view){
|
||||||
TextView text=view.findViewById(R.id.error_text);
|
TextView text=view.findViewById(R.id.error_text);
|
||||||
text.setText(error);
|
text.setText(view.getContext().getString(messageResource, error));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showToast(Context context){
|
public void showToast(Context context){
|
||||||
if(context==null)
|
if(context==null)
|
||||||
return;
|
return;
|
||||||
Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, context.getString(messageResource, error), Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -755,4 +755,9 @@
|
||||||
<string name="view_favorites">View favorites</string>
|
<string name="view_favorites">View favorites</string>
|
||||||
<string name="undo_reblog">Undo boost</string>
|
<string name="undo_reblog">Undo boost</string>
|
||||||
<string name="undo_favorite">Undo favorite</string>
|
<string name="undo_favorite">Undo favorite</string>
|
||||||
|
<string name="could_not_reach_server">Could not reach the server. Make sure you\'re connected to the internet and try again.</string>
|
||||||
|
<string name="connection_timed_out">Connection timed out. Make sure you\'re connected to the internet and try again.</string>
|
||||||
|
<string name="server_error">There may be a problem with your instance or your internet. Make sure you have access to the internet and try again.</string>
|
||||||
|
<string name="not_found">Not found. It probably doesn\'t exist anymore.</string>
|
||||||
|
<string name="unknown_error">Unknown error: %s</string>
|
||||||
</resources>
|
</resources>
|
Loading…
Reference in New Issue