-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
115 additions
and
118 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
206 changes: 103 additions & 103 deletions
206
attendance/src/main/java/com/shalzz/attendance/wrapper/MyVolleyErrorHelper.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,103 @@ | ||
/* | ||
* Copyright (c) 2013-2016 Shaleen Jain <[email protected]> | ||
* | ||
* This file is part of UPES Academics. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.shalzz.attendance.wrapper; | ||
|
||
import android.content.Context; | ||
|
||
import com.android.volley.AuthFailureError; | ||
import com.android.volley.NetworkError; | ||
import com.android.volley.NetworkResponse; | ||
import com.android.volley.ServerError; | ||
import com.android.volley.TimeoutError; | ||
import com.android.volley.VolleyError; | ||
import com.shalzz.attendance.R; | ||
|
||
public class MyVolleyErrorHelper { | ||
/** | ||
* Returns appropriate message which is to be displayed to the user | ||
* against the specified error object. | ||
* | ||
* @param error {@link Object} | ||
* @param context {@link android.content.Context} | ||
* @return Error Message | ||
*/ | ||
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 {@link Object} | ||
* @return True or False | ||
*/ | ||
private static boolean isNetworkProblem(Object error) { | ||
return error instanceof NetworkError; | ||
} | ||
/** | ||
* Determines whether the error is related to server | ||
* @param error {@link Object} | ||
* @return True or False | ||
*/ | ||
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 {@link Object} | ||
* @param context {@link android.content.Context} | ||
* @return Error Message | ||
*/ | ||
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: | ||
if(error.getMessage() != null) | ||
return error.getMessage(); | ||
return "Unauthorized"; | ||
case 407: | ||
return context.getResources().getString(R.string.proxy_error); | ||
|
||
default: | ||
return context.getResources().getString(R.string.generic_server_down); | ||
} | ||
} | ||
return context.getResources().getString(R.string.generic_error); | ||
} | ||
} | ||
/* | ||
* Copyright (c) 2013-2016 Shaleen Jain <[email protected]> | ||
* | ||
* This file is part of UPES Academics. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.shalzz.attendance.wrapper; | ||
|
||
import android.content.Context; | ||
|
||
import com.android.volley.AuthFailureError; | ||
import com.android.volley.NetworkError; | ||
import com.android.volley.NetworkResponse; | ||
import com.android.volley.ServerError; | ||
import com.android.volley.TimeoutError; | ||
import com.android.volley.VolleyError; | ||
import com.shalzz.attendance.R; | ||
|
||
public class MyVolleyErrorHelper { | ||
|
||
/** | ||
* Returns appropriate message which is to be displayed to the user | ||
* against the specified error object. | ||
* | ||
* @param error {@link Object} | ||
* @param context {@link android.content.Context} | ||
* @return Error Message | ||
*/ | ||
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 {@link Object} | ||
* @return True or False | ||
*/ | ||
private static boolean isNetworkProblem(Object error) { | ||
return error instanceof NetworkError; | ||
} | ||
|
||
/** | ||
* Determines whether the error is related to server | ||
* @param error {@link Object} | ||
* @return True or False | ||
*/ | ||
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 {@link Object} | ||
* @param context {@link android.content.Context} | ||
* @return Error Message | ||
*/ | ||
private static String handleServerError(Object err, Context context) { | ||
VolleyError error = (VolleyError) err; | ||
|
||
NetworkResponse response = error.networkResponse; | ||
|
||
if (response != null) { | ||
switch (response.statusCode) { | ||
case 401: | ||
if(error.getMessage() != null) | ||
return error.getMessage(); | ||
return context.getResources().getString(R.string.auth_error); | ||
case 404: | ||
case 422: | ||
case 407: | ||
return context.getResources().getString(R.string.proxy_error); | ||
|
||
default: | ||
return context.getResources().getString(R.string.generic_server_down); | ||
} | ||
} | ||
return context.getResources().getString(R.string.generic_error); | ||
} | ||
} |
This file contains 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