Firebase Cloud Function: How to fail a retryable event without causing a cold start?

Viewed 109

Given a Firebase Cloud Function, what's the proper way to fail an event (with the intention of retrying it) without causing a cold start?

This documentation suggests that unhandled errors cause cold starts. When using async functionality I'm assuming this applies to a rejected Promise as well.

throw new Error('I failed you'); // Will cause a cold start if not caught

Given that functions will often rely on third-party services that may not be 100% reliable, I don't want my functions to incur further penalties from cold starts due to transient downstream errors.

Is this even a recommended strategy? I'm using Firestore so I've considered storing retryable events in a temp collection that is scraped by a scheduled cloud function, but this seems unnecessarily complex.

1 Answers

A rejected promise on its own is not an "unhandled error". What you're showing is a thrown exception and doesn't have anything directly to do with promises. A thrown exception that's escapes the function callback is considered unhandled. If you need to knowingly fail the function invocation, just return a rejected promise, don't throw anything as your quoted example code is doing now.

A simple rejected promise can be created if necessary using Promise.reject():

return Promise.reject(new Error('fail'))
Related