【问题标题】:How to troubleshoot/get error response when you run invalid BigQuery jobs?运行无效的 BigQuery 作业时如何排查/获取错误响应?
【发布时间】:2025-12-18 22:45:01
【问题描述】:

在这段代码中,我尝试在不存在的表上运行选择,getJobReference() 返回NULL,我很想捕捉这种错误,并希望以某种方式获得错误消息。

发生故障时如何获取错误信息?

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
//$client->setDeveloperKey(API_KEY);

if (isset($_SESSION['service_token'])) {
    $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
        $service_account_name, array(
    'https://www.googleapis.com/auth/bigquery',
        ), $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
var_dump($_SESSION);
$bq = new Google_Service_Bigquery($client);

//build query
$sql = 'select * from example.table LIMIT 10';

$job = new Google_Service_Bigquery_Job();
$config = new Google_Service_Bigquery_JobConfiguration();
$queryConfig = new Google_Service_Bigquery_JobConfigurationQuery();
$config->setQuery($queryConfig);

$job->setConfiguration($config);
$queryConfig->setQuery($sql);

$insert = new Google_Service_Bigquery_Job($bq->jobs->insert(PROJECT_ID, $job));
$jr = $insert->getJobReference();
var_dump($jr);/*THIS RETURNS NULL */
$jobId = $jr['jobId'];

$res = new Google_Service_Bigquery_GetQueryResultsResponse($bq->jobs->getQueryResults(PROJECT_ID, $jobId));

//see the results made it as an object ok:
var_dump($res);

【问题讨论】:

标签: php google-bigquery google-api-php-client


【解决方案1】:

他们的 API 会自动动态创建一些类,并在创建时吃掉错误。

我在调试过程后得到如下错误:

try {
    $job = $bq->jobs->insert(PROJECT_ID, $job);
    $status = new Google_Service_Bigquery_JobStatus();
    $status = $job->getStatus();
//    print_r($status);
    if ($status->count() != 0) {
        $err_res = $status->getErrorResult();
        die($err_res->getMessage());
    }
} catch (Google_Service_Exception $e) {
    echo $e->getMessage();
    exit;
}

一般的方法是查看你正在使用的服务是否有Status 类。只有在抛出错误时才会激活 Exception 块,即dryRun 处于活动状态。

$config->setDryRun(true);

【讨论】:

    【解决方案2】:

    我对这个SDK不是很了解,但是你检查过有没有异常吗?

    try {
        $insert = new Google_Service_Bigquery_Job($bq->jobs->insert(PROJECT_ID, $job));
    }
    catch (Exception $e) //or probably better Google_Exception
    {
        print('Something went wrong');
    }
    

    【讨论】:

      最近更新 更多