You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to grab references to uncaught exceptions #215
To be clear: We expect you to be able to use process.on('uncaughtException', function (err) {}); since this is a standard Node.js API that you can access within Cloud Functions
So, the process.on will work. thanks for that. Our ask around 'onunhandledrejection` is for async handlers. However, if async handlers are not possible, we can skip on that ask.
I'm going to loop back with the Functions team about this.
Would a delay of 5 seconds or so before sending the HTTP response on unhandledException fix the issue? Then your additional unhandledException handler may work.
@marshall-lee Can you perhaps fork this repo and try to alter the framework such that the integration works on your end? If you need instructions for deploying a forked version on to Cloud Functions, let me know. You can deploy the forked version on Cloud Run more easily since you have full control of the container. See this blogpost for an example.
Having 1+ user defined async global error handlers would be fine, but I don't think there's a concrete proposed design. Can you describe a bit more how you're going to start the framework?
Questions:
Are we starting the framework as a module? Will that work with you? This affects the design as a user just specifies their function target.
Are you sure process events/exception handling works (process.on), or are you looking to wrap a function in a try/catch where the catch executes a user-defined function?
Which specific Node errors are you trying to catch? Are they specific like unhandledRejection, or all errors?
constframework=require('@google-cloud/functions-framework');// Listen to https://nodejs.org/api/process.html#process_event_uncaughtexceptionframework.on('uncaughtException',async(err)=>{awaitflush(timeout);};
Note: This would only solve users that use this library as a module (not a CLI/bin).
I was creating a prototype, but the design most common way of using this library as a CLI isn't described.
It would also help me if you had an example piece of code that is expected to throw some exception so I can try things out. Here are some I created:
constprocess=require('process');// Test uncaughtExceptionprocess.on('uncaughtException',()=>{console.log('test uncaughtException');});// Test exitprocess.on('exit',()=>{console.log('test exit');});thrownewError('oh, hello there!');// TEST SIGINTprocess.on('SIGINT',()=>{console.log('test exit');});leti=0;while(i<100000000){++i;}
Are we starting the framework as a module? Will that work with you? This affects the design as a user just specifies their function target.
For now I only tested my drafted approach starting functions-framework as a binary locally on my computer. Everything worked fine. Why do you think it doesn't work with binary?
Are you sure process events/exception handling works (process.on), or are you looking to wrap a function in a try/catch where the catch executes a user-defined function?
and
Which specific Node errors are you trying to catch? Are they specific like unhandledRejection, or all errors?
With these global exception handlers I'm trying to catch any exception thrown outside of a function. Such exceptions are rare but they must be caught and sent to Sentry too.
As for exceptions thrown during execution of a function, we catch them using domain.active.on('error', ...) leveraging the fact that framework creates a domain for each incoming request. This helps us to provide a proper context to a caught exception when sending it to Sentry.
When wrapping functions, we both use try ... catch and domain.active.on('error'). The former is for synchronous errors, the latter is for asynchronous exceptions occured during function execution.
This is the index.js file which I start simply with functions-framework --target=helloWorld:
// index.jsconstSentry=require("@sentry/serverless");constframework=require("@google-cloud/functions-framework");Sentry.GCPFunction.init({dsn: "http://123123123123123123123123123@localhost:9000/5",tracesSampleRate: 1.0,});// This piece of code must be somewhere in `Sentry.GCPFunction.init`:constorig=framework.ERROR_HANDLER.onUncaughtExceptionCallback;framework.ERROR_HANDLER.onUncaughtExceptionCallback=function(err){console.log(`CAUGHT ${err}`);Sentry.captureException(err);Sentry.flush().then(()=>{orig(err);});}setTimeout(()=>{thrownewError('bad luck');},10);exports.helloWorld=Sentry.GCPFunction.wrapHttpFunction((req,res)=>{res.send('ok');});
@grant I like your suggested interface more! Mine is just a PoC illustration.
Yours is also better in a sense that you could do Promise.all([sleep(timeout), ...errorPromises]) to avoid hanging of a process.
For now I only tested my drafted approach starting functions-framework as a binary locally on my computer. Everything worked fine. Why do you think it doesn't work with binary?
I still don't have a full picture of a full sample for a function.
Thanks for the sample.
This PR #229 (comment) uses the Functions Framework as a module with require('@google-cloud/functions-framework') rather than the typical CLI we have in our docs/package json script, npx @google-cloud/functions-framework --target=helloWorld.
From the above sample, it looks like you're wishing to require the module as well.
With these global exception handlers I'm trying to catch any exception thrown outside of a function. Such exceptions are rare but they must be caught and sent to Sentry too.
When wrapping functions, we both use try ... catch and domain.active.on('error'). The former for synchronous exceptions and promise rejections, the latter for asynchronous exceptions occured during function execution.
Thanks.
I think we should implement this interface:
constframework=require('@google-cloud/functions-framework');// Listen to https://nodejs.org/api/process.html#process_event_uncaughtexceptionframework.on('uncaughtException',async(err)=>{awaitflush(timeout);};
For that to work, with the use case of I think we have to delay registering existing global error handler when this library is used as a module (i.e. required).
Activity
grant commentedon Sep 15, 2020
Hi @ajjindal, I'm not able to reproduce the issue.
Can you provide a clear, minimal setup for reproducing this issue?
For example, couldn't you just wrap your function in a try/catch? Are you saying the framework itself is throwing an exception that you want to catch?
For creating a global exception handler, I believe we could use Express middleware to register a global exception handler to all routes.
I have a blogpost with using middleware:
https://medium.com/google-cloud/express-routing-with-google-cloud-functions-36fb55885c68
Let me know how to help, or what you're looking for specifically.
ajjindal commentedon Sep 15, 2020
To clarify, this is not a bug but more like an ask to support handling of uncaught exceptions through third party like Sentry.
I am looking for a pointer to the handler to be able to grab uncaught exceptions and report them into Sentry.
Steps to reproduce:
Gave more details to Steren and Vinod.
grant commentedon Sep 15, 2020
OK, thanks for some more details. Would it be okay to catch uncaught exceptions with the Node process and handle them with Sentry?
Or listen to before the process exits:
https://nodejs.org/docs/latest-v12.x/api/process.html
I believe these event handlers can be sync or async functions.
ajjindal commentedon Sep 16, 2020
Sorry for late response. I was checking on the recommendation with our tech lead.
Yes, that +
onunhandledrejectionwould be perfect.grant commentedon Sep 16, 2020
I'm not sure what you mean by this. With the above suggestion, you can catch all Node exceptions in a function handler.
What do you mean by
onunhandledrejection? Where is the exception coming from?If the exception is not coming from the Functions Framework itself, then can it be caught with the function handler?
steren commentedon Sep 16, 2020
To be clear: We expect you to be able to use
process.on('uncaughtException', function (err) {});since this is a standard Node.js API that you can access within Cloud Functionsajjindal commentedon Sep 16, 2020
oh that makes sense. we use the combination in our Node SDK but your suggestion makes sense. Will love to get
process.on.steren commentedon Sep 16, 2020
process.oncan be used today in your SDK.ajjindal commentedon Sep 16, 2020
I must be missing something. Let me discuss internally and get back to the thread here.
ajjindal commentedon Sep 16, 2020
So, the
process.onwill work. thanks for that. Our ask around 'onunhandledrejection` is for async handlers. However, if async handlers are not possible, we can skip on that ask.Thank you for prompt responses.
grant commentedon Sep 24, 2020
@ajjindal Is there anything else regarding grabbing uncaught exceptions that we can do?
#215 (comment) provides some code samples and links to docs.
If not, can I close this issue?
8 remaining items
grant commentedon Oct 1, 2020
I'm going to loop back with the Functions team about this.
Would a delay of 5 seconds or so before sending the HTTP response on
unhandledExceptionfix the issue? Then your additionalunhandledExceptionhandler may work.https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/master/src/invoker.ts#L255
@marshall-lee Can you perhaps fork this repo and try to alter the framework such that the integration works on your end? If you need instructions for deploying a forked version on to Cloud Functions, let me know. You can deploy the forked version on Cloud Run more easily since you have full control of the container. See this blogpost for an example.
grant commentedon Oct 9, 2020
Re: #215 (comment)
@ajjindal @marshall-lee Can you let us know what change you're looking for, or if this solution would work?
ajjindal commentedon Oct 12, 2020
@marshall-lee is planning on creating a PR in the next couple days.
marshall-lee commentedon Oct 19, 2020
@grant @ajjindal
Hi! I've drafted my idea in #229.
ajjindal commentedon Oct 20, 2020
@grant let us know if this approach will work.
grant commentedon Oct 21, 2020
Pardon the delay.
Having 1+ user defined async global error handlers would be fine, but I don't think there's a concrete proposed design. Can you describe a bit more how you're going to start the framework?
Questions:
process.on), or are you looking to wrap a function in atry/catchwhere the catch executes a user-defined function?unhandledRejection, or all errors?Proposed interface
Suggested interface
I was creating a prototype, but the design most common way of using this library as a CLI isn't described.
https://github.com/GoogleCloudPlatform/functions-framework-nodejs/compare/grant_custom_errors
It would also help me if you had an example piece of code that is expected to throw some exception so I can try things out. Here are some I created:
marshall-lee commentedon Oct 21, 2020
For now I only tested my drafted approach starting
functions-frameworkas a binary locally on my computer. Everything worked fine. Why do you think it doesn't work with binary?and
With these global exception handlers I'm trying to catch any exception thrown outside of a function. Such exceptions are rare but they must be caught and sent to Sentry too.
As for exceptions thrown during execution of a function, we catch them using
domain.active.on('error', ...)leveraging the fact that framework creates a domain for each incoming request. This helps us to provide a proper context to a caught exception when sending it to Sentry.When wrapping functions, we both use
try ... catchanddomain.active.on('error'). The former is for synchronous errors, the latter is for asynchronous exceptions occured during function execution.look
https://github.com/getsentry/sentry-javascript/blob/18bce3cb0c6052b61b46d245597ced345ecb14a8/packages/serverless/src/gcpfunction/http.ts#L60-L64
and
https://github.com/getsentry/sentry-javascript/blob/18bce3cb0c6052b61b46d245597ced345ecb14a8/packages/serverless/src/gcpfunction/events.ts#L41-L58
marshall-lee commentedon Oct 21, 2020
This is the
index.jsfile which I start simply withfunctions-framework --target=helloWorld:marshall-lee commentedon Oct 21, 2020
@grant I like your suggested interface more! Mine is just a PoC illustration.
Yours is also better in a sense that you could do
Promise.all([sleep(timeout), ...errorPromises])to avoid hanging of a process.grant commentedon Oct 21, 2020
I still don't have a full picture of a full sample for a function.Thanks for the sample.
This PR #229 (comment) uses the Functions Framework as a module with
require('@google-cloud/functions-framework')rather than the typical CLI we have in our docs/package json script,npx @google-cloud/functions-framework --target=helloWorld.From the above sample, it looks like you're wishing to require the module as well.
OK.
Thanks.
I think we should implement this interface:
For that to work, with the use case of I think we have to delay registering existing global error handler when this library is used as a module (i.e. required).
Functions Framework Interface
functions-framework-nodejs/src/index.ts
Lines 109 to 117 in 01b7df3
...and user code:
framework.onwould amend callback functions to the global process event handlers:functions-framework-nodejs/src/invoker.ts
Lines 254 to 281 in 01b7df3
grant commentedon Oct 21, 2020
The thing with starting the framework with...
...is that currently the Functions Framework registers error handlers immediately, so you cannot add custom handlers.
That's why we'd need to register handlers and start separately: