Skip to content

Commit

Permalink
Added some generic error messages to API to improve error handling mo…
Browse files Browse the repository at this point in the history
…dularity. #34
  • Loading branch information
steenburgh committed Apr 9, 2016
1 parent f2a8c09 commit c460f9d
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions app/src/main/java/com/hvzhub/app/API/API.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.hvzhub.app.API;

import com.hvzhub.app.API.model.APIError;
import com.hvzhub.app.OnLogoutListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
Expand All @@ -18,6 +22,8 @@
import java.util.Date;
import java.util.TimeZone;

import retrofit2.Response;

/**
* A wrapper for the JSON API
*/
Expand Down Expand Up @@ -59,4 +65,66 @@ public static Date dateFromUtcString(String dateStr) throws ParseException {
public static String utcStringFromDate(Date date) {
return DateConverter.getInstance().serialize(date);
}

/**
* Check if the response contains a message indicating that the current session id is invalid
* and log them out if needed.
*
* @param context
* @param response
* @param logoutListener
* @return True if the response indicates that the sessionId is valid.
* False if the sessionId is invalid and the user has been logged out.
*/
public static boolean checkForInvalidSessionIdMsg(Context context, Response response, final OnLogoutListener logoutListener) {
APIError apiError = ErrorUtils.parseError(response);
String err = apiError.error.toLowerCase();
if (err.contains(context.getString(R.string.invalid_session_id))) {
// Notify the parent activity that the user should be logged out
// Don't bother stopping the loading animation
logoutListener.onLogout();
return false;
}
return true;
}

public static void displayUnexpectedResponseError(Context ctx, final OnRetryListener listener) {
AlertDialog.Builder b = new AlertDialog.Builder(ctx);
b.setTitle(ctx.getString(R.string.unexpected_response))
.setMessage(ctx.getString(R.string.unexpected_response_hint))
.setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.OnRetry();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}

public static void displayGenericConnectionError(Context ctx, final OnRetryListener listener) {
AlertDialog.Builder b = new AlertDialog.Builder(ctx);
b.setTitle(ctx.getString(R.string.generic_connection_error))
.setMessage(ctx.getString(R.string.generic_connection_error_hint))
.setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.OnRetry();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}

public interface OnRetryListener {
void OnRetry();
}
}

0 comments on commit c460f9d

Please sign in to comment.