문제

In this code, I try to run a select on a table that doesn't exists, getJobReference() returns NULL and I would love to catch this kind of error, and would like to error messages obtained somehow.

How to obtain the error message when something fails?

$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);
도움이 되었습니까?

해결책

Their API automatically creates some classes on fly, and errors are eaten on creation.

I ended up after a debug process to get errors like this:

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;
}

The general way is to see if there is a Status class for the service you are using. The Exception block gets activated only when errors are thrown, and that is when dryRun is active.

$config->setDryRun(true);

다른 팁

I don't know this SDK very well, but did you check if you get an exception?

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');
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top