javascriptnode.jsfirebase-realtime-databasefirebase-admin

Firebase RTDB Completion Callback Not Catching Errors


I am missing something in what the completion callback actually captures, as far as errors are concerned. When I pass bad data to the update call, it simply errors out and stops processing.

export async function set (seller) {
    const updates = {};
    
    updates[ `sellers/12345/name-first` ] = seller.firstName;

    // I have tried this both as async/await and with then/catch
    return await database.ref().set(updates, error => { return error; });
};

This will produce the error Error: update failed: value argument contains an invalid key (sellers/12345/name-first) as one would expect, but my function does not return. It stops processing as it would on any unhandled error. According to the documentation, this is how to set it up, but it seems to ignore it. I have even tried putting it in a try-catch block. I have tried invalid keys and undefined data, but neither will let the update function return as expected. Good data returns as expected.

If the completion callback cannot catch invalid keys or undefined data, what kind of errors will it catch? More importantly, if the try-catch won't even catch the errors, then how can they be handled? This is why I have got to be missing something, IMHO.

I am running express 4.21, Node 22.11.0, and firebase-admin 13.0.1.


Solution

  • My guess is that this error is rejected by the client, while the callback is only used for rejections that come from the server.

    It is weird that a try-catch doesn't catch the error though, as I expect the SDK to throw a (local) exception for this.

    This seems to work for me:

    try {
      ref.set(updates);
    }
    catch (e) {
      console.error("catch block");
    }
    

    The catch block gets triggered

    The minimal repro I used to test this: https://jsbin.com/momemet/edit?js,console