user3508995
user3508995

Reputation: 187

NodeJS Promises do not return AWS Athena Error

I've written a web interface to allow users to query AWS Athena. It is using NodeJS Promises with Async and Await, but the error handling is not good. When a user enters an invalid SQL query (table not found), the response is always "{}". I've tried Athena-express and Athena-client. I then tried the Athena-client "toStream()" syntax.

var stream = client.execute('SELECT * from sometable limit 1').toStream()
stream.on('data', function(record) { console.log('record='+JSON.stringify(record)) })
stream.on('query_end', function(queryExecution) { console.log(queryExecution) })
stream.on('end', function() { console.log('end') })
stream.on('error', function(e) { console.error(e) })

And it returned an error. I thought great, let me encapsulate it in a promise. I've used:

new Promise((resolve, reject) => ...
require('stream/promises');
require('stream-promise');

Essentially, the moment I put it in a promise, the error I get back is always "{}". Does anyone know how you can get the actual Athena error returned using promises without it crashing the app?

let executeQuery = new Promise((resolve, reject) =>
{
    let response = client.execute('SELECT * from sometable limit 1').toStream();
    response.on('error', (err) => { reject(err); })
    response.on('finish', function() { resolve(response); })
})

async function run()
{
    try
    {
        let response = await executeQuery;
        console.log('response='+JSON.stringify(response));
    }
    catch(e) { console.log('MY error='+JSON.stringify(e)) }
}
run();

It returns "My error={}", and I need it to return "Table sometable does not exist"

Upvotes: 0

Views: 302

Answers (1)

user3508995
user3508995

Reputation: 187

Found my issue. I can't JSON.stringify(e) I have to JSON.stringify(e.message)

const output = JSON.stringify(e.message);

That was annoying!

Upvotes: 1

Related Questions