问题排查

跟踪 GRPC 问题

准备工作

在写入日志文件之前,请务必初始化并启用写入权限。在以下示例中,日志文件名为 /var/log/grpc.log

touch /var/log/grpc.log
chmod 666 /var/log/grpc.log

如果遇到文件权限问题,则可能需要在上述命令前面加上 sudo

记录到文件

如需将 gRPC 日志保存到文件中,请将以下行添加到 php.ini 文件中:

grpc.grpc_verbosity=debug
grpc.grpc_trace=all,-timer_check
grpc.log_filename=/var/log/grpc.log

如需将日志写入 stderr,请排除上一示例中的最后一行。如需详细了解其他选项,请参阅 gRPC 环境变量

错误详情

Google Cloud 服务在返回错误时遵循 AIP-193 准则。Google Cloud 服务使用一组已定义的标准错误载荷,可在 google.rpc.error_details.proto 中找到。这些指南涵盖了最常见的 API 错误,例如配额失败和参数无效。

PHP 客户端库发出的 API 错误包含 messagecodestatusdetails 字段。在 details 字段中,reasondomainmetadata 字段中会显示其他 google.rpc.ErrorInfo 数据。以下示例展示了如果您在启用 Translate API 之前使用云项目调用 Translation 库时可能会出现的错误:

Google\ApiCore\ApiException: {
    "reason": "SERVICE_DISABLED",
    "domain": "googleapis.com",
    "errorInfoMetadata": {
        "consumer": "projects\PROJECT_ID",
        "service": "translate.googleapis.com"
    },
    "message": "Cloud Translation API has not been used in project PROJECT_ID before or it is disabled. Enable it by visiting https:\/\/console.developers.google.com\/apis\/api\/translate.googleapis.com\/overview?project=PROJECT_ID then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "code": 7,
    "status": "PERMISSION_DENIED",
    "details": [
        {
            "@type": "google.rpc.errorinfo-bin",
            "reason": "SERVICE_DISABLED",
            "domain": "googleapis.com",
            "metadata": {
                "consumer": "projects\PROJECT_ID",
                "service": "translate.googleapis.com"
            }
        },
        {
            "@type": "google.rpc.help-bin",
            "links": [
                {
                    "description": "Google developers console API activation",
                    "url": "https:\/\/console.developers.google.com\/apis\/api\/translate.googleapis.com\/overview?project=PROJECT_ID"
                }
            ]
        }
    ]
}

如需详细了解 Google API 的常规错误模型,请参阅 Cloud API 错误