/* 
 * Copyright 2014 OpenMarket Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.matrix.androidsdk.rest.callback;

import android.util.Log;

import org.matrix.androidsdk.rest.model.MatrixError;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class RestAdapterCallback<T> implements Callback<T> {

    private static final String LOG_TAG = "RestAdapterCallback";

    private ApiCallback apiCallback;

    public RestAdapterCallback(ApiCallback apiCallback) {
        this.apiCallback = apiCallback;
    }

    @Override
    public void success(T t, Response response) {
        apiCallback.onSuccess(t);
    }

    /**
     * Default failure implementation that calls the right error handler
     * @param error
     */
    @Override
    public void failure(RetrofitError error) {
        Log.e(LOG_TAG, error.getMessage() + " url=" + error.getUrl());
        if (error.isNetworkError()) {
            apiCallback.onNetworkError(error);
        }
        else {
            // Try to convert this into a Matrix error
            MatrixError mxError;
            try {
                mxError = (MatrixError) error.getBodyAs(MatrixError.class);
            }
            catch (Exception e) {
                mxError = null;
            }
            if (mxError != null) {
                apiCallback.onMatrixError(mxError);
            }
            else {
                apiCallback.onUnexpectedError(error);
            }
        }
    }
}

@@ -1159,33 +1159,289 @@
     
-apiCallback.onSuccess(t);
+// add try catch to prevent application crashes while managing destroyed object%0A        try %7B%0A            apiCallback.onSuccess(t);%0A        %7D catch (Exception e) %7B%0A            Log.e(LOG_TAG, %22Exception success %22 + e.getMessage() + %22 while managing %22 + response.getUrl());%0A        %7D
 %0A   
@@ -1719,24 +1719,46 @@
 kError()) %7B%0A
+            try %7B%0A    
             
@@ -1784,32 +1784,196 @@
 rkError(error);%0A
+            %7D catch (Exception e) %7B%0A                Log.e(LOG_TAG, %22Exception NetworkError %22 + e.getMessage() + %22 while managing %22 + error.getUrl());%0A            %7D%0A
         %7D%0A      
@@ -2288,24 +2288,50 @@
  != null) %7B%0A
+                try %7B%0A    
             
@@ -2366,24 +2366,199 @@
 r(mxError);%0A
+                %7D catch (Exception e) %7B%0A                    Log.e(LOG_TAG, %22Exception MatrixError %22 + e.getMessage() + %22 while managing %22 + error.getUrl());%0A                %7D%0A
             
@@ -2570,32 +2570,58 @@
          else %7B%0A
+                try %7B%0A    
                 
@@ -2650,32 +2650,211 @@
 edError(error);%0A
+                %7D catch (Exception e) %7B%0A                    Log.e(LOG_TAG, %22Exception UnexpectedError %22 + e.getMessage() + %22 while managing %22 + error.getUrl());%0A                %7D%0A
             %7D%0A  

Add exception trapping to avoid application crashes while managing request responses.