Created
December 3, 2020 11:21
-
-
Save virendersran01/13d8b91b1e532ae61dfdd4655334f5d3 to your computer and use it in GitHub Desktop.
VolleyErrorHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VolleyErrorHelper { | |
/** | |
* Returns appropriate message which is to be displayed to the user | |
* against the specified error object. | |
* | |
* @param error | |
* @param context | |
* @return | |
*/ | |
public static String getMessage(Object error, Context context) { | |
if (error instanceof TimeoutError) { | |
return context.getResources().getString(R.string.generic_server_down); | |
} | |
else if (isServerProblem(error)) { | |
return handleServerError(error, context); | |
} | |
else if (isNetworkProblem(error)) { | |
return context.getResources().getString(R.string.no_internet); | |
} | |
return context.getResources().getString(R.string.generic_error); | |
} | |
/** | |
* Determines whether the error is related to network | |
* @param error | |
* @return | |
*/ | |
private static boolean isNetworkProblem(Object error) { | |
return (error instanceof NetworkError) || (error instanceof NoConnectionError); | |
} | |
/** | |
* Determines whether the error is related to server | |
* @param error | |
* @return | |
*/ | |
private static boolean isServerProblem(Object error) { | |
return (error instanceof ServerError) || (error instanceof AuthFailureError); | |
} | |
/** | |
* Handles the server error, tries to determine whether to show a stock message or to | |
* show a message retrieved from the server. | |
* | |
* @param err | |
* @param context | |
* @return | |
*/ | |
private static String handleServerError(Object err, Context context) { | |
VolleyError error = (VolleyError) err; | |
NetworkResponse response = error.networkResponse; | |
if (response != null) { | |
switch (response.statusCode) { | |
case 404: | |
case 422: | |
case 401: | |
try { | |
// server might return error like this { "error": "Some error occured" } | |
// Use "Gson" to parse the result | |
HashMap<String, String> result = new Gson().fromJson(new String(response.data), | |
new TypeToken<Map<String, String>>() { | |
}.getType()); | |
if (result != null && result.containsKey("error")) { | |
return result.get("error"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
// invalid request | |
return error.getMessage(); | |
default: | |
return context.getResources().getString(R.string.generic_server_down); | |
} | |
} | |
return context.getResources().getString(R.string.generic_error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment