What does the throw new Error line do here in this context?

whatwhatwhat

I have found an example of a function that deletes a record in some collection. I modified it to do what I've always been doing for error handling - return a returnVal and a returnMsg. However, I just noticed that it also has the line throw new Error("An error occurred while blocking user.");. If I plan on using this function in my index.ts file as a cloud function for my Firebase project, what will this line do? If there is an error, then my 2 outputs should be enough to tell the calling function what happened, but I'm not sure what else this throw line will do. If it completely exits the execution of the process, that's not good. I want to handle the flow of what happens on error myself.

/**
 * Deletes the friend record with matching uid from the /users/{BlockedUID}/friends collection.
 * @param {string} uid - The uid of the user who is blocking the friend.
 * @param {string} blockedUid - The uid of the user who is being blocked.
 * @return {Promise<{returnVal: number, returnMsg: string}>} A Promise that resolves with an object containing the return value and message.
 */
export async function blockUser(
  uid: string,
  blockedUid: string
): Promise<{ returnVal: number; returnMsg: string }> {
  try {
    // Delete friend record where uid = input uid
    await admin
      .firestore()
      .collection(`users/${blockedUid}/friends`)
      .doc(uid)
      .delete();
    console.log(`User with uid ${uid} successfully blocked.`);
    return { returnVal: 1, returnMsg: "User successfully blocked." };
  } catch (error) {
    console.error(error);
    throw new Error("An error occurred while blocking user.");
  }
}
Doug Stevenson

but I'm not sure what else this throw line will do. If it completely exits the execution of the process, that's not good.

Firstly, JavaScript documentation is easy to find, and you can find out what throw will do. From MDN:

The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.

If you don't want the program to terminate, you should use try/catch the capture error yourself and decide what to do from there.

The try...catch statement is comprised of a try block and either a catch block, a finally block, or both. The code in the try block is executed first, and if it throws an exception, the code in the catch block will be executed. The code in the finally block will always be executed before control flow exits the entire construct.

So, if you want to control what happens in the call to blockUser, you should use try/catch.

Or, you could just write the code you want to execute any one you want if the throw doesn't work for you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive