firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.01k stars 925 forks source link

INTERNAL error message when calling annotateImage cloud vision function #5502

Closed nchitnis1 closed 1 year ago

nchitnis1 commented 1 year ago

[REQUIRED] Environment info

firebase-tools: 11.22.0

Platform: macOS

[REQUIRED] Test case

I am following the instructions at firebase.google.com/docs/ml/ios/recognize-text, to get recognized text back, but I am seeing the following errors:

Firebase error code = FunctionsErrorCode(rawValue: 13)
Firebase error message = INTERNAL

[REQUIRED] Steps to reproduce

Followed these steps below, but I had to make the changes listed in https://github.com/firebase/firebase-tools/issues/5486 to get it to work.

git clone https://github.com/firebase/functions-samples
cd vision-annotate-images
cd functions
npm install
cd ..
firebase init
firebase deploy --only functions:annotateImage

The output looks good:

✔  functions: Finished running predeploy script.
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
✔  functions: required API cloudbuild.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged /Users/nitin/dev1/learn_mlkit/functions-samples/vision-annotate-images/functions (153.02 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: updating Node.js 16 function annotateImage(us-central1)...
✔  functions[annotateImage(us-central1)] Successful update operation.
i  functions: cleaning up build files...

✔  Deploy complete!

Here is my Swift code, most of which I copied from firebase.google.com/docs/ml/ios/recognize-text

       func authenticate() {

        Auth.auth().signInAnonymously { authResult, _ in

            guard let user = authResult?.user else { return }
            let isAnonymous = user.isAnonymous // true
            let uid = user.uid
            print("signInAnonymously finished isAnonymous=\(isAnonymous) uid=\(uid)")
        }
    }

    func firebaseRecognize() {

        do {
            guard let cameraInputImage = try ImageMgr.getJPGImageFromBundle(named: "test_image")
            else {
                print("Failed to create UIImage from test_image.jpg")
                return
            }

            guard let imageData = cameraInputImage.jpegData(compressionQuality: 1.0) else { return }
            let base64encodedImage = imageData.base64EncodedString()

            lazy var functions = Functions.functions()

            let requestData = [
                "image": ["content": base64encodedImage],
                "features": ["type": "DOCUMENT_TEXT_DETECTION"],
                "imageContext": ["languageHints": ["en"]]
            ]

            functions.httpsCallable("annotateImage").call(requestData) { result, error in

                if let error = error as NSError? {

                    if error.domain == FunctionsErrorDomain {

                        if let code = FunctionsErrorCode(rawValue: error.code) {
                            print("Firebase error code = \(code)")
                        }

                        print("Firebase error message = \(error.localizedDescription)")

                        if let details = error.userInfo[FunctionsErrorDetailsKey] {
                            print("Firebase error details = \(details)")
                        }
                    }
                    else {
                        print("Firebase error, some other domain")
                    }
                }
                else {

                    print("Firebase Function completed succesfully")

                    guard let annotation = (result?.data as? [String: Any])?["fullTextAnnotation"] as? [String: Any] else { return }
                    print("%nComplete annotation:")
                    let text = annotation["text"] as? String ?? ""
                    print("%n\(text)")
                }
            }
        }
        catch {
            print("Error in recognize")
        }
    }

[REQUIRED] Expected behavior

Should authenticate successfully, then should return the recognized text

[REQUIRED] Actual behavior

My authentication function authenticate() works, and it prints out the first line, but I am seeing the following errors printed from the function firebaseRecognize() shown above:

signInAnonymously finished isAnonymous=true uid=DN1Fy5IEYHeO4k22GlZ7413649q2
Firebase error code = FunctionsErrorCode(rawValue: 13)
Firebase error message = INTERNAL
colerogers commented 1 year ago

@nchitnis1 Can you find the Cloud Functions logs and paste them here? You can find them by navigating to the Firebase console, click on Functions, and click on the logs view link that will navigate you to the Google Cloud Logs Explorer page.

nchitnis1 commented 1 year ago

There were 3 logs, I am pasting them below

Function execution started

{
  "textPayload": "Function execution started",
  "insertId": "yp1cb1ej1zxc",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "project_id": "myprogram",
      "region": "us-central1",
      "function_name": "annotateImage"
    }
  },
  "timestamp": "2023-02-10T17:18:14.855705886Z",
  "severity": "DEBUG",
  "labels": {
    "execution_id": "8bltpxlffr3z"
  },
  "logName": "projects/myprogram/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
  "trace": "projects/myprogram/traces/ff32483817543f97dc647659d3ad34c6",
  "receiveTimestamp": "2023-02-10T17:18:14.862253032Z"
}

Callable request verification passed

{
  "insertId": "63e67c590009c2cf136fc1af",
  "jsonPayload": {
    "verifications": {
      "app": "MISSING",
      "auth": "VALID"
    },
    "message": "Callable request verification passed"
  },
  "resource": {
    "type": "cloud_function",
    "labels": {
      "project_id": "myprogram",
      "function_name": "annotateImage",
      "region": "us-central1"
    }
  },
  "timestamp": "2023-02-10T17:18:17.639695Z",
  "severity": "DEBUG",
  "labels": {
    "instance_id": "00c61b117cd8d3ca00f7839765823080aa5c4309032f93e1d4c0b2c89497326e98612cd72f8d69a3b220e0ed06a6dec4afec45a9443a152596",
    "execution_id": "8bltpxlffr3z",
    "firebase-log-type": "callable-request-verification"
  },
  "logName": "projects/myprogram/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
  "trace": "projects/myprogram/traces/ff32483817543f97dc647659d3ad34c6",
  "receiveTimestamp": "2023-02-10T17:18:17.871985066Z"
}

Function execution took 2792 ms, finished with status code: 500

{
  "textPayload": "Function execution took 2792 ms, finished with status code: 500",
  "insertId": "1do9liyefun05",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "project_id": "myprogram",
      "region": "us-central1",
      "function_name": "annotateImage"
    }
  },
  "timestamp": "2023-02-10T17:18:17.648691187Z",
  "severity": "DEBUG",
  "labels": {
    "execution_id": "8bltpxlffr3z"
  },
  "logName": "projects/myprogram/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
  "trace": "projects/myprogram/traces/ff32483817543f97dc647659d3ad34c6",
  "receiveTimestamp": "2023-02-10T17:18:17.871985066Z"
}
colerogers commented 1 year ago

It looks like something is happening during the execution of the annotateImage in the function - https://github.com/firebase/functions-samples/blob/539345b1d7b3af7a7fd4fadaa54255329bdff3e3/vision-annotate-images/functions/src/index.ts#L23

Could you log out the error that is occurring and re-deploy the function? You can just do something like this:

...
try {
    return await client.annotateImage(JSON.parse(data));
  } catch (e) {
    // Add logging here
    console.log(e);
    throw new functions.https.HttpsError("internal", e.message, e.details);
  }
....
nchitnis1 commented 1 year ago

I added the log statement, and did a deploy (see output below) but I did not see anything. As a test, I changed the HttpsError function parameters with the parameters "test" to see if it is actually calling this changed function. (See code below). I don't see the new "test" output, when I run my Swift app, I continue to see the old output ( INTERNAL error). So this function is not being called.

When "firebase deploy --only functions:annotateImage" is run the file functions/lib/index.js is being overwritten, with the file in the functions/src/index.ts. But after this, it is not clear what is happening.

  try {

    return await client.annotateImage(JSON.parse(data));
  } catch (e: any) {

    //Added logging
    console.log(e);

     throw new functions.https.HttpsError("internal", "test", "test");

    //throw new functions.https.HttpsError("internal", e.message, e.details);
  }

When I run "firebase deploy --only functions:annotateImage" it completes successfully, see the output below:

✔  functions: Finished running predeploy script.
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔  functions: required API cloudfunctions.googleapis.com is enabled
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
✔  functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged /Users/nitin/dev1/learn_mlkit/functions-samples/vision-annotate-images/functions (153.13 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: updating Node.js 16 function annotateImage(us-central1)...
✔  functions[annotateImage(us-central1)] Successful update operation.
i  functions: cleaning up build files...

✔  Deploy complete!
jakubrak commented 1 year ago

The error is in the cloud function, there is redundant JSON.parse() call (data is already an object). It should be: return await client.annotateImage(data);

colerogers commented 1 year ago

@jakubrak Oh nice catch, I'll update the sample code to yank that out. Thanks!

colerogers commented 1 year ago

@nchitnis1 Removing that function call & redeploying should fix your Cloud Functions issue

 ...
try {
    return await client.annotateImage(data);
  } catch (e) {
    throw new functions.https.HttpsError("internal", e.message, e.details);
  }
....