Google Cloud Functions - 权限被拒绝与失败的前提条件

Google Cloud Functions - permission-denied vs failed-precondition

在检查授权 ID 是否存在时,我应该使用哪种 HTTPs 错误类型?

const userId = context.auth.token.uid;

if (!userId) {
  throw new functions.https.HttpsError(
    "failed-precondition",
    "Authentication required."
  );
}

const userId = context.auth.token.uid;

if (!userId) {
  throw new functions.https.HttpsError(
    "permission-denied",
    "Authentication required."
  );
}

实际上您可以在 list of available error types 中选择您喜欢的错误类型。事实上,这个错误代码将被发送回您通常控制的前端。

现在,查看错误代码列表...

permission-denied: The caller does not have permission to execute the specified operation.

failed-precondition: Operation was rejected because the system is not in a state required for the operation's execution.

...我会选择permission-denied.