[Error] : Refactor Toast error management to split network error and server error

This commit is contained in:
Olivier Bouillet 2020-09-24 22:26:21 +02:00
parent 95353ca673
commit 62ae5c66d6
9 changed files with 45 additions and 10 deletions

View File

@ -37,6 +37,7 @@ import net.schueller.peertube.R;
import net.schueller.peertube.adapter.ChannelAdapter; import net.schueller.peertube.adapter.ChannelAdapter;
import net.schueller.peertube.adapter.VideoAdapter; import net.schueller.peertube.adapter.VideoAdapter;
import net.schueller.peertube.helper.APIUrlHelper; import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.helper.ErrorHelper;
import net.schueller.peertube.helper.MetaDataHelper; import net.schueller.peertube.helper.MetaDataHelper;
import net.schueller.peertube.model.Account; import net.schueller.peertube.model.Account;
import net.schueller.peertube.model.Avatar; import net.schueller.peertube.model.Avatar;
@ -206,7 +207,7 @@ public class AccountActivity extends CommonActivity {
} }
} else { } else {
Toast.makeText(AccountActivity.this, getString(R.string.api_error), Toast.LENGTH_SHORT).show(); ErrorHelper.showToastFromCommunicationError( AccountActivity.this, null );
} }
@ -215,7 +216,7 @@ public class AccountActivity extends CommonActivity {
@Override @Override
public void onFailure(@NonNull Call<Account> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<Account> call, @NonNull Throwable t) {
Log.wtf(TAG, t.fillInStackTrace()); Log.wtf(TAG, t.fillInStackTrace());
Toast.makeText(AccountActivity.this, getString(R.string.api_error), Toast.LENGTH_SHORT).show(); ErrorHelper.showToastFromCommunicationError( AccountActivity.this, t );
} }
}); });
@ -247,8 +248,7 @@ public class AccountActivity extends CommonActivity {
} }
} else{ } else{
Toast.makeText(AccountActivity.this, getString(R.string.api_error), Toast.LENGTH_SHORT).show(); ErrorHelper.showToastFromCommunicationError( AccountActivity.this, null );
} }
isLoadingVideos = false; isLoadingVideos = false;
@ -258,7 +258,7 @@ public class AccountActivity extends CommonActivity {
@Override @Override
public void onFailure(@NonNull Call<VideoList> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<VideoList> call, @NonNull Throwable t) {
Log.wtf("err", t.fillInStackTrace()); Log.wtf("err", t.fillInStackTrace());
Toast.makeText(AccountActivity.this, getString(R.string.api_error), Toast.LENGTH_SHORT).show(); ErrorHelper.showToastFromCommunicationError( AccountActivity.this, t );
isLoadingVideos = false; isLoadingVideos = false;
swipeRefreshLayoutVideos.setRefreshing(false); swipeRefreshLayoutVideos.setRefreshing(false);
} }
@ -281,7 +281,7 @@ public class AccountActivity extends CommonActivity {
} else { } else {
Toast.makeText(AccountActivity.this, getString(R.string.api_error), Toast.LENGTH_SHORT).show(); ErrorHelper.showToastFromCommunicationError( AccountActivity.this, null );
} }
@ -290,7 +290,7 @@ public class AccountActivity extends CommonActivity {
@Override @Override
public void onFailure(@NonNull Call<ChannelList> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<ChannelList> call, @NonNull Throwable t) {
Log.wtf(TAG, t.fillInStackTrace()); Log.wtf(TAG, t.fillInStackTrace());
Toast.makeText(AccountActivity.this, getString(R.string.api_error), Toast.LENGTH_SHORT).show(); ErrorHelper.showToastFromCommunicationError( AccountActivity.this, t );
} }
}); });
} }

View File

@ -31,6 +31,7 @@ import android.widget.TextView;
import net.schueller.peertube.R; import net.schueller.peertube.R;
import net.schueller.peertube.helper.APIUrlHelper; import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.helper.ErrorHelper;
import net.schueller.peertube.model.Avatar; import net.schueller.peertube.model.Avatar;
import net.schueller.peertube.model.Me; import net.schueller.peertube.model.Me;
import net.schueller.peertube.network.GetUserService; import net.schueller.peertube.network.GetUserService;
@ -162,6 +163,7 @@ public class MeActivity extends CommonActivity {
@Override @Override
public void onFailure(@NonNull Call<Me> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<Me> call, @NonNull Throwable t) {
ErrorHelper.showToastFromCommunicationError( MeActivity.this, t );
account.setVisibility(View.GONE); account.setVisibility(View.GONE);
} }
}); });

View File

@ -35,6 +35,7 @@ import android.widget.Toast;
import net.schueller.peertube.R; import net.schueller.peertube.R;
import net.schueller.peertube.adapter.ServerSearchAdapter; import net.schueller.peertube.adapter.ServerSearchAdapter;
import net.schueller.peertube.helper.APIUrlHelper; import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.helper.ErrorHelper;
import net.schueller.peertube.model.ServerList; import net.schueller.peertube.model.ServerList;
import net.schueller.peertube.network.GetServerListDataService; import net.schueller.peertube.network.GetServerListDataService;
import net.schueller.peertube.network.RetrofitInstance; import net.schueller.peertube.network.RetrofitInstance;
@ -171,7 +172,7 @@ public class SearchServerActivity extends CommonActivity {
@Override @Override
public void onFailure(@NonNull Call<ServerList> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<ServerList> call, @NonNull Throwable t) {
Log.wtf("err", t.fillInStackTrace()); Log.wtf("err", t.fillInStackTrace());
Toast.makeText(SearchServerActivity.this, getString(R.string.api_error), Toast.LENGTH_SHORT).show(); ErrorHelper.showToastFromCommunicationError( SearchServerActivity.this, t );
isLoading = false; isLoading = false;
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
} }

View File

@ -59,6 +59,7 @@ import com.mikepenz.iconics.IconicsDrawable;
import net.schueller.peertube.R; import net.schueller.peertube.R;
import net.schueller.peertube.adapter.VideoAdapter; import net.schueller.peertube.adapter.VideoAdapter;
import net.schueller.peertube.helper.APIUrlHelper; import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.helper.ErrorHelper;
import net.schueller.peertube.model.Video; import net.schueller.peertube.model.Video;
import net.schueller.peertube.model.VideoList; import net.schueller.peertube.model.VideoList;
import net.schueller.peertube.network.GetUserService; import net.schueller.peertube.network.GetUserService;
@ -372,7 +373,7 @@ public class VideoListActivity extends CommonActivity {
@Override @Override
public void onFailure(@NonNull Call<VideoList> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<VideoList> call, @NonNull Throwable t) {
Log.wtf("err", t.fillInStackTrace()); Log.wtf("err", t.fillInStackTrace());
Toast.makeText(VideoListActivity.this, getString(R.string.api_error), Toast.LENGTH_SHORT).show(); ErrorHelper.showToastFromCommunicationError( VideoListActivity.this, t );
isLoading = false; isLoading = false;
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
} }

View File

@ -39,6 +39,7 @@ import com.squareup.picasso.Picasso;
import net.schueller.peertube.R; import net.schueller.peertube.R;
import net.schueller.peertube.helper.APIUrlHelper; import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.helper.ErrorHelper;
import net.schueller.peertube.helper.MetaDataHelper; import net.schueller.peertube.helper.MetaDataHelper;
import net.schueller.peertube.intents.Intents; import net.schueller.peertube.intents.Intents;
import net.schueller.peertube.model.Account; import net.schueller.peertube.model.Account;
@ -122,6 +123,7 @@ public class VideoMetaDataFragment extends Fragment {
@Override @Override
public void onFailure(Call<Rating> call, Throwable t) { public void onFailure(Call<Rating> call, Throwable t) {
ErrorHelper.showToastFromCommunicationError( getActivity(), t );
// Do nothing. // Do nothing.
} }
}); });

View File

@ -59,6 +59,7 @@ import com.mikepenz.iconics.Iconics;
import net.schueller.peertube.R; import net.schueller.peertube.R;
import net.schueller.peertube.helper.APIUrlHelper; import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.helper.ErrorHelper;
import net.schueller.peertube.model.File; import net.schueller.peertube.model.File;
import net.schueller.peertube.model.Video; import net.schueller.peertube.model.Video;
import net.schueller.peertube.network.GetVideoDataService; import net.schueller.peertube.network.GetVideoDataService;
@ -196,7 +197,7 @@ public class VideoPlayerFragment extends Fragment implements VideoRendererEventL
@Override @Override
public void onFailure(@NonNull Call<Video> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<Video> call, @NonNull Throwable t) {
Log.wtf(TAG, t.fillInStackTrace()); Log.wtf(TAG, t.fillInStackTrace());
Toast.makeText(context, "Something went wrong: " + t.getLocalizedMessage(), Toast.LENGTH_LONG).show(); ErrorHelper.showToastFromCommunicationError( getActivity(), t );
} }
}); });
} }

View File

@ -0,0 +1,26 @@
package net.schueller.peertube.helper;
import android.content.Context;
import android.widget.Toast;
import net.schueller.peertube.R;
import java.io.IOException;
import retrofit2.HttpException;
public class ErrorHelper {
public static void showToastFromCommunicationError( Context context, Throwable throwable ) {
if (throwable instanceof IOException ) {
//handle network error
Toast.makeText( context, context.getString( R.string.network_error), Toast.LENGTH_SHORT).show();
} else if (throwable instanceof HttpException ) {
//handle HTTP error response code
Toast.makeText(context, context.getString(R.string.api_error), Toast.LENGTH_SHORT).show();
} else {
//handle other exceptions
Toast.makeText(context, context.getString(R.string.api_error), Toast.LENGTH_SHORT).show();
}
}
}

View File

@ -109,6 +109,7 @@
<string name="account_about_description">Description :</string> <string name="account_about_description">Description :</string>
<string name="account_about_joined">Inscrit·e le :</string> <string name="account_about_joined">Inscrit·e le :</string>
<string name="api_error">Quelque chose sest mal passé, veuillez essayer plus tard !</string> <string name="api_error">Quelque chose sest mal passé, veuillez essayer plus tard !</string>
<string name="network_error">Problème réseau, veuillez vérifier votre connection</string>
<string name="action_bar_title_server_selection">Sélectionnez un serveur</string> <string name="action_bar_title_server_selection">Sélectionnez un serveur</string>
<string name="ab">abkhaze</string> <string name="ab">abkhaze</string>
<string name="aa">afar</string> <string name="aa">afar</string>

View File

@ -315,6 +315,7 @@
<string name="account_about_description">Description:</string> <string name="account_about_description">Description:</string>
<string name="account_about_joined">Joined:</string> <string name="account_about_joined">Joined:</string>
<string name="api_error">Something went wrong, please try later!</string> <string name="api_error">Something went wrong, please try later!</string>
<string name="network_error">Network access error, please check your connectivity</string>
<string name="action_bar_title_server_selection">Select Server</string> <string name="action_bar_title_server_selection">Select Server</string>
<string name="hello_blank_fragment">Hello blank fragment</string> <string name="hello_blank_fragment">Hello blank fragment</string>
<string name="action_bar_title_address_book"/> <string name="action_bar_title_address_book"/>