FaisalAhmed
FaisalAhmed

Reputation: 3651

Ignoring exception from a finished function in Cloud Functions

I am using Cloud Functions for Firebase to get access token using this and after that i am doing rest call to https://www.googleapis.com/auth/cloud-platform. But while doing so i got exception Ignoring exception from a finished function.

exception I want to know why i am getting this message and what is the reason behind it. #AskFirebase

Edited below is my accessTokenHandler.js

 const functions = require('firebase-functions');
 var googleAuth = require('google-oauth-jwt');
 const predictor=require("../prediction/predictor");


module.exports.getAccessToken=() =>{

googleAuth.authenticate({
email: 'my.gserviceaccount.com',
keyFile: "./accesstoken/key2.pem",
expiration: 3600000,
scopes: ['https://www.googleapis.com/auth/cloud-platform']
}, function (err, token) {
if(token){
    console.log("token:"+token);
    predictor.predict(token);
}
 if(err)console.log("err:"+err);
});
}

and below is my predictor.js

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

module.exports.predict=(accessToken) =>{
predictImage(accessToken);
}

function predictImage(accessToken){

var httpRequest= new XMLHttpRequest();
httpRequest.open("POST","url",true);
httpRequest.setRequestHeader("Content-Type","application/json");
httpRequest.setRequestHeader("Accept","application/json");
httpRequest.setRequestHeader("Authorization", "Bearer " + accessToken); 
httpRequest.send(getRequestJson());

httpRequest.onreadystatechange =function(){
    if(this.readyState==4){
    console.log("status:"+this.status+" state:"+this.readyState)
    console.log("response:"+this.responseText)
    }
}
}

function getRequestJson()
{
var b64Image='/9j/oAxt--encoded---f/2Q==';
var requestJson={"instances":
[{"key":"input_cloudfunction.jpg","image_bytes":{"b64":b64Image}}]};
return requestJson;
}

and my index.js file

const functions = require('firebase-functions');

exports.handleFreshAccessToken = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!"); 
 const accessHandler=require("./accesstoken/accesstoken_handler");
 return accessHandler.getAccessToken();
});

Upvotes: 6

Views: 6281

Answers (3)

smoore4
smoore4

Reputation: 4866

If you use throw new Error(Error reason here.) you may get a log entry such as this:

Exception from a finished function: Error: Error reason here.

The answers are correct that you can use Promises to wait for all possible processing to finish, but you also have the option of formatting your error message as json in order to specifiy a Severity level, for example:

exports.helloPubSub = (event, context) => {
  console.log('console log');
  console.error('console error');
  //throw new Error('throw error')

  // The function will end before setTimout finishes
  const timer = setTimeout(function() {
    // Create a log entry.
    const entry = Object.assign(
    {
      severity: 'ERROR',
      message: 'Test timeout error.',
      component: 'arbitrary-property',
    }
  );
  // Serialize to json and log
  console.log(JSON.stringify(entry));

  }, 5 * 1000);
};

This will show up as an Error in Logs Explorer:

enter image description here

Upvotes: 0

gregn3
gregn3

Reputation: 1774

Apparently uncaught exceptions from async functions are not displayed in the Cloud Functions framework. The solution is to surround the code with a try {} catch block, await for the async code, and log all exceptions in the catch block.

For example:

async function ()
{
    try
    {
        // throws exception
        await SomeFunc ()
    }
    catch (e)
    {
        console.log ('error: ' + e)
    }
}

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317322

There is some error happening in your code before it finished. For HTTPS type functions, they formally finish when a response is sent to the client. In your code, you're sending a response immediately to the client, which means everything else you're doing after that is occurring "after the function finished".

If you have async work to do in a function, you should wait on all that to complete (using promises) before sending the response.

Upvotes: 6

Related Questions