Skip to content

Commit

Permalink
#259 implemented broadcasting error with the server's response when t…
Browse files Browse the repository at this point in the history
…he response status code is not 2xx. Modified BroadcastReceiver and Delegate onError public API.
  • Loading branch information
gotev committed Jul 28, 2017
1 parent 96b2941 commit b25882f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void onProgress(Context context, UploadInfo uploadInfo) {
}

@Override
public void onError(Context context, UploadInfo uploadInfo, Exception exception) {
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, Exception exception) {

}

Expand Down Expand Up @@ -105,7 +105,7 @@ public void onProgress(Context context, UploadInfo uploadInfo) {
}

@Override
public void onError(Context context, UploadInfo uploadInfo, Exception exception) {
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, Exception exception) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public void onProgress(Context context, UploadInfo uploadInfo) {
}

@Override
public void onError(Context context, UploadInfo uploadInfo, Exception exception) {

public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, Exception exception) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void onReceive(Context context, Intent intent) {

switch (data.getStatus()) {
case ERROR:
onError(context, data.getUploadInfo(), data.getException());
onError(context, data.getUploadInfo(), data.getServerResponse(), data.getException());
break;

case COMPLETED:
Expand Down Expand Up @@ -87,7 +87,8 @@ public void onProgress(final Context context, final UploadInfo uploadInfo) {
}

@Override
public void onError(final Context context, final UploadInfo uploadInfo, final Exception exception) {
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, Exception exception) {

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ public interface UploadStatusDelegate {
*
* @param context context
* @param uploadInfo upload status information
* @param exception exception that caused the error
* @param serverResponse response got from the server. It can be null if the server has not
* responded or if the request has not reached the server due to a
* networking problem.
* @param exception exception that caused the error. It can be null if the request successfully
* reached the server, but it responded with a 4xx or 5xx status code.
*/
void onError(final Context context, final UploadInfo uploadInfo, final Exception exception);
void onError(final Context context, final UploadInfo uploadInfo,
final ServerResponse serverResponse, final Exception exception);

/**
* Called when the upload is completed successfully. Override this method to add your own logic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void run() {
*/
protected final void broadcastCompleted(final ServerResponse response) {

boolean successfulUpload = ((response.getHttpCode() / 100) == 2);
final boolean successfulUpload = ((response.getHttpCode() / 100) == 2);

if (successfulUpload) {
onSuccessfulUpload();
Expand All @@ -256,7 +256,8 @@ protected final void broadcastCompleted(final ServerResponse response) {
}
}

Logger.debug(LOG_TAG, "Broadcasting upload completed for " + params.getId());
Logger.debug(LOG_TAG, "Broadcasting upload " + (successfulUpload ? "completed" : "error")
+ " for " + params.getId());

final UploadInfo uploadInfo = new UploadInfo(params.getId(), startTime, uploadedBytes,
totalBytes, (attempts - 1),
Expand All @@ -268,12 +269,16 @@ protected final void broadcastCompleted(final ServerResponse response) {
mainThreadHandler.post(new Runnable() {
@Override
public void run() {
delegate.onCompleted(service, uploadInfo, response);
if (successfulUpload) {
delegate.onCompleted(service, uploadInfo, response);
} else {
delegate.onError(service, uploadInfo, response, null);
}
}
});
} else {
BroadcastData data = new BroadcastData()
.setStatus(BroadcastData.Status.COMPLETED)
.setStatus(successfulUpload ? BroadcastData.Status.COMPLETED : BroadcastData.Status.ERROR)
.setUploadInfo(uploadInfo)
.setServerResponse(response);

Expand Down Expand Up @@ -409,7 +414,7 @@ private void broadcastError(final Exception exception) {
mainThreadHandler.post(new Runnable() {
@Override
public void run() {
delegate.onError(service, uploadInfo, exception);
delegate.onError(service, uploadInfo, null, exception);
}
});
} else {
Expand Down

0 comments on commit b25882f

Please sign in to comment.