Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading runtime error when deploying firebase functions on emulator #6183

Open
leo-paz opened this issue Jul 25, 2023 · 0 comments
Open

Comments

@leo-paz
Copy link

leo-paz commented Jul 25, 2023

Environment info

firebase-tools: 12.4.2
2nd gen firebase functions

Platform: macOS
node-fetch: 3.3.1

Test case

import and use node fetch in 2nd gen function.

import * as functions from "firebase-functions"
import { defineSecret } from "firebase-functions/params";
import { HttpsError, onCall } from "firebase-functions/v2/https";
import fetch, { Headers } from 'node-fetch';
const { defineInt, defineString } = require('firebase-functions/params');

const sanityProjectId = defineString('SANITY_PROJECT_ID');
const sanityApiVersion = defineInt('SANITY_API_VERSION');
const sanityToken = defineSecret('SANITY_TOKEN');

// function for modifying feedback type in sanity
export const submitFeedbackToSanity = onCall(
  { cors: ["*"], secrets: [sanityToken] }, 
  async request => {
  if (!request.auth) {
    // Throwing an HttpsError so that the client gets the error details.
    throw new HttpsError(
      "failed-precondition",
      "The function must be called while authenticated."
    )
  }

  const { feedback, prompt, userId, chatId } = request.data
  functions.logger.log("feedback", feedback)

  // make http call to sanity api
  const sanityUrl = `https://${sanityProjectId.value()}.sanity.studio/v${sanityApiVersion.value()}/data/mutate/production`

  const sanityMutation = {
    mutations: [
      {
        create: {
          _type: "chatFeedback",
          title: (prompt as String).substring(0, 100),
          prompt,
          userId,
          feedback,
          chatId,
        }
      }
    ]
  }

  const sanityHeaders = {
    "Content-Type": "application/json",
    Authorization: `Bearer ${sanityToken.value()}`
  }

  // return fetch(sanityUrl, {
  //   method: "POST",
  //   headers: new Headers(sanityHeaders),
  //   body: JSON.stringify(sanityMutation)
  // })
  const test = await fetch(sanityUrl)
  return;
})

Steps to reproduce

Simply run the emulators from the CLI and I get an error. Commenting out the use of fetch(sanityUrl) gives me no errors.

Expected behavior

That emulators start with no errors regardless if I am using fetch(sanityUrl)

Actual behavior

I get a very misleading error that I already mentioned here: #6148 (comment)

but to repeat it:

[debug] [2023-07-25T03:59:09.040Z] Got response code 400; body Failed to generate manifest from function source: Error: The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.

I don't see how it has anything to do with the above error and it would be great if the runtime was more accurate. I'm assuming it somehow tries to restart my functions and thats when i run into that error? but I only call initializeApp() once ever in my functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants