Flutterfire: [CloudFunctions] [Local Emulator] PlatformException(functionsError, Cloud function failed with exception., {code: INTERNAL, details: null, message: INTERNAL})

Created on 18 Jun 2020  Â·  12Comments  Â·  Source: FirebaseExtended/flutterfire

Describe the bug
Callable function works remotely, but using Functions Emulator throws always the following error:

E/flutter ( 7013): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: PlatformException(functionsError, Cloud function failed with exception., {code: INTERNAL, details: null, message: INTERNAL})

To Reproduce
Call the callable function from flutter:

    var _cloudFunction = CloudFunctions(region: 'us-central1');
    _cloudFunction.useFunctionsEmulator(origin: "http://localhost:5001")
        .getHttpsCallable(functionName: 'functionsgroup-myFunction')
        .call();

Expected behavior
No error.

Additional context

No record in the emulator logs of the function being called. Whilst if I enter the URL in my browser I do see the logs recording it.


functions packages.json

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "debug": "npm run build && firebase emulators:start --only functions --inspect-functions",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"
  },
  "engines": {
    "node": "10"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1",
    "firebase-auth": "^0.1.2",
    "typeorm": "^0.2.25",
    "express": "^4.17.1",
    "reflect-metadata": "^0.1.13",
    "mysql2": "^2.1.0"
  },
  "devDependencies": {
    "@types/node": "^10.17.26",
    "firebase-functions-test": "^0.2.1",
    "ts-node": "^8.10.2",
    "tslint": "^6.1.2",
    "typescript": "^3.9.5"
  },
  "private": true
}


functions firebase.json

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ]
  },
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  },
  "emulators": {
    "functions": {
      "host": "0.0.0.0",
      "port": 5001
    },
    "firestore": {
      "host": "0.0.0.0",
      "port": 8080
    }
  }
}

Flutter doctor


functions firebase.json

[√] Flutter (Channel beta, 1.19.0-4.1.pre, on Microsoft Windows [Version 10.0.19042.330], locale en-GB)
    • Flutter version 1.19.0-4.1.pre at C:\src\flutter
    • Framework revision f994b76974 (9 days ago), 2020-06-09 15:53:13 -0700
    • Engine revision 9a28c3bcf4
    • Dart version 2.9.0 (build 2.9.0-14.1.beta)


[!] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
    • Android SDK at C:\Users\jesus\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[!] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] VS Code (version 1.46.0)
    • VS Code at C:\Users\jesus\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.11.0

[√] Connected device (3 available)
    • sdk gphone x86 • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Web Server     • web-server    • web-javascript • Flutter Tools
    • Chrome         • chrome        • web-javascript • Google Chrome 83.0.4103.106

! Doctor found issues in 2 categories.

crowd functions bug

Most helpful comment

Same issue on iOS.. So this fix won't work.

All 12 comments

Hi @gsusI
Can you please flutter run --verbose and cloud_functions version
Thank you

Hi @gsusI
Can you please flutter run --verbose and cloud_functions version
Thank you

This was actually the straw that broke the camel's back for us. The black-boxy nature of diagnosing these types of errors is just less appealing that managing a stack of infrastructure ourselves.

I figured I'd share what we were getting to help fix this for people using this in the future:

[+35245 ms] E/flutter ( 4831): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(functionsError, Cloud function failed with exception., {code: INTERNAL, details: null, message: INTERNAL})
[        ] E/flutter ( 4831): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
[        ] E/flutter ( 4831): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
[        ] E/flutter ( 4831): <asynchronous suspension>
[        ] E/flutter ( 4831): #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
[        ] E/flutter ( 4831): #3      MethodChannelCloudFunctions.callCloudFunction (package:cloud_functions_platform_interface/src/method_channel_cloud_functions.dart:43:15)
[        ] E/flutter ( 4831): #4      HttpsCallable.call (package:cloud_functions/src/https_callable.dart:33:12)
[        ] E/flutter ( 4831): #5      searchCardShares.<anonymous closure> (package:project_b/data/action-creators.dart:114:37)
[        ] E/flutter ( 4831): #6      thunkMiddleware (package:redux_thunk/redux_thunk.dart:45:18)
[        ] E/flutter ( 4831): #7      Store._createDispatchers.<anonymous closure> (package:redux/src/store.dart:255:43)
[        ] E/flutter ( 4831): #8      Store.dispatch (package:redux/src/store.dart:267:27)
[        ] E/flutter ( 4831): #9      CardCollection.build.<anonymous closure> (package:project_b/screens/card-collection.dart:31:23)
[        ] E/flutter ( 4831): #10     _NeumorphicButtonState.build.<anonymous closure> (package:project_b/widgets/neumorphic-button.dart:30:18)
[        ] E/flutter ( 4831): #11     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
[        ] E/flutter ( 4831): #12     TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:504:11)
[        ] E/flutter ( 4831): #13     BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:282:5)
[        ] E/flutter ( 4831): #14     BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:254:7)
[        ] E/flutter ( 4831): #15     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
[        ] E/flutter ( 4831): #16     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
[        ] E/flutter ( 4831): #17     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
[        ] E/flutter ( 4831): #18     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
[        ] E/flutter ( 4831): #19     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
[        ] E/flutter ( 4831): #20     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
[        ] E/flutter ( 4831): #21     _rootRunUnary (dart:async/zone.dart:1196:13)
[        ] E/flutter ( 4831): #22     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
[        ] E/flutter ( 4831): #23     _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)
[        ] E/flutter ( 4831): #24     _invoke1 (dart:ui/hooks.dart:275:10)
[        ] E/flutter ( 4831): #25     _dispatchPointerDataPacket (dart:ui/hooks.dart:184:5)
[        ] E/flutter ( 4831): 

I have this issue as well. If any help is needed to recreate it (additional stack traces, etc), I'm happy to assist.

Is there any update regarding the issue?
I had to re-create the callable functions as https functions to test them with functions emulator.

I was using the functions to connect with my physical device over my local IP. Tried almost every thing but could not get it to work. At last had to re-create them as https functions to get it working with Firestore emulator.

@gsusI This solved my issue: https://github.com/FirebaseExtended/flutterfire/issues/2834#issuecomment-660717624

I added a file at main/res/xml/network_security_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="false">10.0.2.2</domain>
    </domain-config>
</network-security-config>

Then I edited my debug manifest at src/debug/AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.package">
    <!-- Flutter needs it to communicate with the running application
         to allow setting breakpoints, to provide hot reload, etc.
    -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:networkSecurityConfig="@xml/network_security_config"/>
</manifest>

So, during debug mode, cleartext traffic is permitted between Android and the Functions Emulator.

I had the same issue, and adding network_security_config.xml and editing the AndroidManifest.xml worked for me as well.

It raises the same exception but does not provide any additional info. Unfortunately, the suggestion proposed by @dustin-graham did not fix it. Anyone still facing this problem?


EDIT: it seems that adding _networkSecurityConfig_ to the main manifest instead of the debug one fixes the problem, thanks again.

Same issue on iOS.. So this fix won't work.

I faced the same error. I solved it using these steps.

I hope this helps someone. I'm willing to make a PR for this and add it under the emulator doc.

  1. Open ios/Runner.xcworkspace in Xcode.
  2. Open Runner/info.plist.
  3. Add a property called App Transport Security Settings.
  4. Right click on that property and click Add Row.
  5. Add a property called Allows Local Networking and enter its value as YES.

It works! Thanks!

Would someone be willing to send up a PR to the docs for the suggestion by @ranjithrd, would be extremely welcome :)

This is the where to add it; https://github.com/FirebaseExtended/flutterfire/blob/master/docs/functions/usage.mdx#emulator-usage could move the current bit there to an Android sub-header and then add the above steps by @ranjithrd under an iOS header.

Please tag me in the PR or here if you pick this up. Thank you 💙

@Salakar I just finished creating a pull request with the changes you'd wanted. It's PR number 4072 (https://github.com/FirebaseExtended/flutterfire/pull/4079).

Thanks.

Was this page helpful?
0 / 5 - 0 ratings