question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception not thrown on syntax error

See original GitHub issue

Environment details

  • OS: MacOS
  • Node.js version: 12.18
  • npm version: 6.14.4
  • @google-cloud/bigquery version: 5.3.0

Steps to reproduce

  it("throws on bigquery syntax error", async () => {
    const bq = new BigQuery({ projectId: "REDACTED" })

    const [job] = await bq.createQueryJob({
      query: `DECLARE foo DEFAULT ( SELECT 1 );
      SELECT 1 FROM XYZ`,
      timeoutMs: 5000,
      location: "US"
    })

    const [rows] = await job.getQueryResults()

    assert.strictEqual(rows.length, 0)
  })

Expected result:

Invalid value: Table name “XYZ” missing dataset while no default dataset is set in the request. at [2:1]

Actual result No exception is thrown, rows.length = 0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
steffnaycommented, Dec 7, 2020

The issue here is that scripting statements like this generate child jobs for each statement. When you call job.getQueryResults using the original parent job, what’s returned is the result of the last statement that executed. So, in the case of your example, it returns the empty results from the DECLARE statement’s execution.

To obtain the results of all statements in the script, you must enumerate the child jobs and call jobs.getQueryResults on each of them. You can access the list of child jobs by setting the parentJobId option in a bigquery.getJobs call.

Here is how you would access the error from your example:

 const bq = new BigQuery({ projectId: "REDACTED" });

 [job] = await bq.createQueryJob({
   query: 
   `DECLARE foo DEFAULT ( SELECT 1 );
   SELECT 1 FROM XYZ`,
   timeoutMs: 5000,
   location: "US"
 });

 // Must still wait for parent job to complete before accessing child jobs
 const [rows] = await job.getQueryResults();

 const [childJobs] = await bq.getJobs({parentJobId: job.id});

 for (const job of childJobs) {
   console.log('Job: ', job.id);
   console.log('Error: ', job.metadata.errorResult);
 }

// Output:
//
// Job:  script_job_123...
// Error:  { reason: 'invalid',
//   location: 'xyz',
//   message: 'Table name "xyz" missing dataset while no default dataset is set in the request.' }
//
// Job:  script_job_678...
// Error:  undefined

Documentation for this concept is here.

0reactions
ro8inmorgancommented, Nov 18, 2020

I actually have a similar problem but not with table name but with an incorrect field name. There’s no error thrown, just an empty result. In the Google Console the job did error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why browser is not throwing exception for syntax error?
I accidentally wrote a wrong JavaScript syntax (I think so). code is var temp = {}; temp.a = 34; height:34, //should fail here....
Read more >
How to Throw Exceptions in Python - Rollbar
The syntax error exception occurs when the code does not conform to Python keywords, naming style, or programming structure.
Read more >
throw - JavaScript - MDN Web Docs - Mozilla
The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be ...
Read more >
Exceptions in Java
A program containing one or more syntax errors is ill-formed. ... execution could result in a checked exception being thrown (but not caught)...
Read more >
Exceptions - Manual - PHP
The thrown object must be an instanceof Throwable. Trying to throw an object that is not will result in a PHP Fatal Error....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found