flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

[cloud_functions] How to get Firebase Functions error Object ?

Open LeonanFragaLeonardo opened this issue 3 years ago • 2 comments

flutter doctor -v

[√] Flutter (Channel unknown, 2.5.0, on Microsoft Windows [Version 10.0.19042.1237], locale en-US)
    • Flutter version 2.5.0 at C:\tools\flutter
    • Upstream repository unknown
    • Framework revision 4cc385b4b8 (3 weeks ago), 2021-09-07 23:01:49 -0700
    • Engine revision f0826da7ef
    • Dart version 2.14.0

flutter pub deps -- --style=compact (abbreviated)

dependencies:
- cloud_firestore 2.5.2 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- cloud_functions 3.0.3 [cloud_functions_platform_interface cloud_functions_web firebase_core firebase_core_platform_interface flutter]
- firebase_auth 3.1.1 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]
- firebase_core 1.6.0 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_crashlytics 2.2.1 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace]
- firebase_performance 0.7.0+7 [firebase_core flutter]
- firebase_storage 10.0.3 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter]

Hi everyone,

Context:

My question concerns about how to handle with API error response when using cloud_functions to perform FirebaseFunctions calls. I'm using cloud_functions v3.0.3 to make some Http requests via Firebase Http Functions.

Usage:

My requests are made by using the following code:

final HttpsCallable callable = firebaseFunctions.httpsCallable(
        'authenticateWithAD',
        options: HttpsCallableOptions(
          timeout: const Duration(
            seconds: 20,
          ),
        ),
      );

      final result = await callable
          .call({"username": user.userName, "password": user.password});
      ....

Everything works fine when response is 2xx. (I didn't test with 1xx or 3xx)

But:

When my API returns an error (4xx or 5xx), I can't get the error object. The FirebaseFunctionsException gives me an object like this:

code: "internal"
details: null
message: "INTERNAL"
plugin: "firebase_functions"
strackTrace: #0       StandardMethodChanne...
hashCode: 
runtimeType: Type...

As we can see, there is no property that I can use to get the error from my API.

My API returns this:

{
  "statusCode":401,
  "name":"InvalidCredentialsError",
  "code":"AUTH_0002",
  "message":"Invalid username or password"
}

Main Question

Is there an way to get the API error response by using callable.call returns from FirebaseFunctionsException ?

LeonanFragaLeonardo avatar Sep 28 '21 12:09 LeonanFragaLeonardo

I'm seeing this issue as well with cloud_functions: ^3.0.3 so labeling it for now. It doesn't seem intended to just throw internal with no other information.

flutter doctor -v
[√] Flutter (Channel master, 2.6.0-12.0.pre.135, on Microsoft Windows [Version 10.0.19043.1237], locale en-GB)
    • Flutter version 2.6.0-12.0.pre.135 at C:\Development\flutter_master
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 3074c9e04b (4 hours ago), 2021-09-28 22:08:04 -0700
    • Engine revision bccb3a57eb
    • Dart version 2.15.0 (build 2.15.0-156.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\marku\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\203.7678000\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

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

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.2)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.31624.102
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\203.7678000
    • Flutter plugin version 60.1.2
    • Dart plugin version 203.8430
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] Connected device (4 available)
    • sdk gphone x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Windows (desktop)       • windows       • windows-x64    • Microsoft Windows [Version 10.0.19043.1237]
    • Chrome (web)            • chrome        • web-javascript • Google Chrome 94.0.4606.61
    • Edge (web)              • edge          • web-javascript • Microsoft Edge 93.0.961.52

• No issues found!

markusaksli-nc avatar Sep 29 '21 09:09 markusaksli-nc

Hi, I'm facing the issue as well.

We are throwing the standard firebase.functions.HttpsError from our node backend and they are serialized into JSON like this.

{
  "details": {
    "type": "missing-property",
    "property": "calendarEventUid"
  },
  "message": "validation failed at $: expected 'calendarEventUid' in object, found: { calendarEventUid_LOLOLOLOL: '6af33f40-6646-4b67-be6f-c7050b46648c' }",
  "status": "INVALID_ARGUMENT"
}

However the details field of the corresponding FirebaseFunctionsException is null when the exception surfaces in flutter.

I guess the problem is somewhere around here, but I don't know how to easily debug this.

fpoppinga avatar Oct 25 '21 11:10 fpoppinga

Hello, Testing with

  throw new functions.https.HttpsError(
    'invalid-argument',
    'Input and Output types did not match.',
    {input: resp, output: 'apple'}
  );

Allows to properly get all the transmitted information. I'll close this, feel free to reopen if you still experience this issue.

Lyokone avatar Sep 27 '22 11:09 Lyokone