From 8b7f7888611b866ea833db8c18526bcbacbe7e1c Mon Sep 17 00:00:00 2001 From: Tomas Radvansky Date: Tue, 25 Dec 2018 14:06:52 +1300 Subject: [PATCH 1/2] Add SocketTimeoutException for Cloud Functions On android, usually much sooner than firebase timeout, socket timeout exception occurs. This is not handled properly and invalid format envelope exception is thrown. Exception is not handled and whole call is interrupted as non handled dart exception --- .../cloudfunctions/CloudFunctionsPlugin.java | 109 ++++++++++-------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/packages/cloud_functions/android/src/main/java/io/flutter/plugins/firebase/cloudfunctions/cloudfunctions/CloudFunctionsPlugin.java b/packages/cloud_functions/android/src/main/java/io/flutter/plugins/firebase/cloudfunctions/cloudfunctions/CloudFunctionsPlugin.java index 3de5a3d6ee0e..d634e560f580 100644 --- a/packages/cloud_functions/android/src/main/java/io/flutter/plugins/firebase/cloudfunctions/cloudfunctions/CloudFunctionsPlugin.java +++ b/packages/cloud_functions/android/src/main/java/io/flutter/plugins/firebase/cloudfunctions/cloudfunctions/CloudFunctionsPlugin.java @@ -5,66 +5,85 @@ package io.flutter.plugins.firebase.cloudfunctions.cloudfunctions; import android.support.annotation.NonNull; + import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.functions.FirebaseFunctions; import com.google.firebase.functions.FirebaseFunctionsException; import com.google.firebase.functions.HttpsCallableReference; import com.google.firebase.functions.HttpsCallableResult; + import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; + +import java.net.SocketTimeoutException; import java.util.HashMap; import java.util.Map; -/** CloudFunctionsPlugin */ +/** + * CloudFunctionsPlugin + */ public class CloudFunctionsPlugin implements MethodCallHandler { - /** Plugin registration. */ - public static void registerWith(Registrar registrar) { - final MethodChannel channel = new MethodChannel(registrar.messenger(), "cloud_functions"); - channel.setMethodCallHandler(new CloudFunctionsPlugin()); - } + /** + * Plugin registration. + */ + public static void registerWith(Registrar registrar) { + final MethodChannel channel = new MethodChannel(registrar.messenger(), "cloud_functions"); + channel.setMethodCallHandler(new CloudFunctionsPlugin()); + } - @Override - public void onMethodCall(MethodCall call, final Result result) { - switch (call.method) { - case "CloudFunctions#call": - String functionName = call.argument("functionName"); - HttpsCallableReference httpsCallableReference = - FirebaseFunctions.getInstance().getHttpsCallable(functionName); - Map parameters = call.argument("parameters"); - httpsCallableReference - .call(parameters) - .addOnCompleteListener( - new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - result.success(task.getResult().getData()); - } else { - if (task.getException() instanceof FirebaseFunctionsException) { - FirebaseFunctionsException exception = - (FirebaseFunctionsException) task.getException(); - Map exceptionMap = new HashMap<>(); - exceptionMap.put("code", exception.getCode().name()); - exceptionMap.put("message", exception.getMessage()); - exceptionMap.put("details", exception.getDetails()); - result.error( - "functionsError", - "Cloud function failed with exception.", - exceptionMap); - } else { - Exception exception = task.getException(); - result.error(null, exception.getMessage(), null); - } - } - } - }); - break; - default: - result.notImplemented(); + @Override + public void onMethodCall(MethodCall call, final Result result) { + switch (call.method) { + case "CloudFunctions#call": + String functionName = call.argument("functionName"); + HttpsCallableReference httpsCallableReference = + FirebaseFunctions.getInstance().getHttpsCallable(functionName); + Map parameters = call.argument("parameters"); + httpsCallableReference + .call(parameters) + .addOnCompleteListener( + new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + result.success(task.getResult().getData()); + } else { + if (task.getException() instanceof FirebaseFunctionsException) { + FirebaseFunctionsException exception = + (FirebaseFunctionsException) task.getException(); + Map exceptionMap = new HashMap<>(); + exceptionMap.put("code", exception.getCode().name()); + exceptionMap.put("message", exception.getMessage()); + exceptionMap.put("details", exception.getDetails()); + result.error( + "functionsError", + "Cloud function failed with exception.", + exceptionMap); + } else if (task.getException() instanceof SocketTimeoutException) { + SocketTimeoutException exception = + (SocketTimeoutException) task.getException(); + Map exceptionMap = new HashMap<>(); + exceptionMap.put("code", FirebaseFunctionsException.Code.DEADLINE_EXCEEDED.name()); + exceptionMap.put("message", exception.getMessage()); + exceptionMap.put("details", "SocketTimeoutException"); + result.error( + "functionsError", + "Cloud function failed with exception.", + exceptionMap); + } else { + Exception exception = task.getException(); + result.error(null, exception.getMessage(), null); + } + } + } + }); + break; + default: + result.notImplemented(); + } } - } } From 3933a0b64d5f806dd4c567f54a4feb99fdbe337c Mon Sep 17 00:00:00 2001 From: Tomas Radvansky Date: Sat, 12 Jan 2019 15:51:07 +1300 Subject: [PATCH 2/2] Update CloudFunctionsPlugin.java --- .../cloudfunctions/CloudFunctionsPlugin.java | 116 +++++++++--------- 1 file changed, 57 insertions(+), 59 deletions(-) diff --git a/packages/cloud_functions/android/src/main/java/io/flutter/plugins/firebase/cloudfunctions/cloudfunctions/CloudFunctionsPlugin.java b/packages/cloud_functions/android/src/main/java/io/flutter/plugins/firebase/cloudfunctions/cloudfunctions/CloudFunctionsPlugin.java index d634e560f580..1e0c98520a08 100644 --- a/packages/cloud_functions/android/src/main/java/io/flutter/plugins/firebase/cloudfunctions/cloudfunctions/CloudFunctionsPlugin.java +++ b/packages/cloud_functions/android/src/main/java/io/flutter/plugins/firebase/cloudfunctions/cloudfunctions/CloudFunctionsPlugin.java @@ -5,14 +5,12 @@ package io.flutter.plugins.firebase.cloudfunctions.cloudfunctions; import android.support.annotation.NonNull; - import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.functions.FirebaseFunctions; import com.google.firebase.functions.FirebaseFunctionsException; import com.google.firebase.functions.HttpsCallableReference; import com.google.firebase.functions.HttpsCallableResult; - import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; @@ -27,63 +25,63 @@ * CloudFunctionsPlugin */ public class CloudFunctionsPlugin implements MethodCallHandler { - /** - * Plugin registration. - */ - public static void registerWith(Registrar registrar) { - final MethodChannel channel = new MethodChannel(registrar.messenger(), "cloud_functions"); - channel.setMethodCallHandler(new CloudFunctionsPlugin()); - } + /** + * Plugin registration. + */ + public static void registerWith(Registrar registrar) { + final MethodChannel channel = new MethodChannel(registrar.messenger(), "cloud_functions"); + channel.setMethodCallHandler(new CloudFunctionsPlugin()); + } - @Override - public void onMethodCall(MethodCall call, final Result result) { - switch (call.method) { - case "CloudFunctions#call": - String functionName = call.argument("functionName"); - HttpsCallableReference httpsCallableReference = - FirebaseFunctions.getInstance().getHttpsCallable(functionName); - Map parameters = call.argument("parameters"); - httpsCallableReference - .call(parameters) - .addOnCompleteListener( - new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - result.success(task.getResult().getData()); - } else { - if (task.getException() instanceof FirebaseFunctionsException) { - FirebaseFunctionsException exception = - (FirebaseFunctionsException) task.getException(); - Map exceptionMap = new HashMap<>(); - exceptionMap.put("code", exception.getCode().name()); - exceptionMap.put("message", exception.getMessage()); - exceptionMap.put("details", exception.getDetails()); - result.error( - "functionsError", - "Cloud function failed with exception.", - exceptionMap); - } else if (task.getException() instanceof SocketTimeoutException) { - SocketTimeoutException exception = - (SocketTimeoutException) task.getException(); - Map exceptionMap = new HashMap<>(); - exceptionMap.put("code", FirebaseFunctionsException.Code.DEADLINE_EXCEEDED.name()); - exceptionMap.put("message", exception.getMessage()); - exceptionMap.put("details", "SocketTimeoutException"); - result.error( - "functionsError", - "Cloud function failed with exception.", - exceptionMap); - } else { - Exception exception = task.getException(); - result.error(null, exception.getMessage(), null); - } - } - } - }); - break; - default: - result.notImplemented(); + @Override + public void onMethodCall(MethodCall call, final Result result) { + switch (call.method) { + case "CloudFunctions#call": + String functionName = call.argument("functionName"); + HttpsCallableReference httpsCallableReference = + FirebaseFunctions.getInstance().getHttpsCallable(functionName); + Map < String, Object > parameters = call.argument("parameters"); + httpsCallableReference + .call(parameters) + .addOnCompleteListener( + new OnCompleteListener < HttpsCallableResult > () { + @Override + public void onComplete(@NonNull Task < HttpsCallableResult > task) { + if (task.isSuccessful()) { + result.success(task.getResult().getData()); + } else { + if (task.getException() instanceof FirebaseFunctionsException) { + FirebaseFunctionsException exception = + (FirebaseFunctionsException) task.getException(); + Map < String, Object > exceptionMap = new HashMap < > (); + exceptionMap.put("code", exception.getCode().name()); + exceptionMap.put("message", exception.getMessage()); + exceptionMap.put("details", exception.getDetails()); + result.error( + "functionsError", + "Cloud function failed with exception.", + exceptionMap); + } else if (task.getException() instanceof SocketTimeoutException) { + SocketTimeoutException exception = + (SocketTimeoutException) task.getException(); + Map < String, Object > exceptionMap = new HashMap < > (); + exceptionMap.put("code", FirebaseFunctionsException.Code.DEADLINE_EXCEEDED.name()); + exceptionMap.put("message", exception.getMessage()); + exceptionMap.put("details", "SocketTimeoutException"); + result.error( + "functionsError", + "Cloud function failed with exception.", + exceptionMap); + } else { + Exception exception = task.getException(); + result.error(null, exception.getMessage(), null); + } } - } + } + }); + break; + default: + result.notImplemented(); + } + } }