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..f7d69066 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,62 @@ 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=0; + } } @Override public void bindErrorView(View view){ TextView text=view.findViewById(R.id.error_text); - text.setText(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, 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 069e370f..5f3886ff 100644 --- a/mastodon/src/main/res/values/strings.xml +++ b/mastodon/src/main/res/values/strings.xml @@ -755,4 +755,8 @@ View favorites Undo boost Undo favorite + 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