tedious
tedious copied to clipboard
Request error hook
Hi,
I have a task where I need to insert data into a table and then nested child data into a separate table, with an incremental index generated in code.
So I have created a function that creates a request for each data row, and then passes the array of requests through to another function that creates a connection and then iterates the array executing the requests in order. However, I can't seem to correctly hook the request error event?
In the following code if I get a request fail, it doesn't fire the request error event.
Should the request object support binding to the error event in this manner?
function runRequest (requests) {
return new Promise((resolve, reject) => {
var connection = new Connection(config)
connection.on('error', (err) => {
reject(err)
})
connection.on('debug', (txt) => {
// console.log(txt)
})
function processNextItem(request) {
request.on('error', function (err) {
reject(err)
});
request.on('requestCompleted', function () {
if(requests.length > 0){
// still have requests to process
processNextItem(requests.shift())
}
else{
console.log('Done, Closing connection')
connection.close()
resolve()
}
});
connection.execSql(request);
}
connection.connect((err) => {
if (err) {
reject(err)
}
processNextItem(requests.shift())
})
})
}
I think this might be due to having too many event's being registered each time you create a new Request object, which if you have > 10 (by default) request objects, the event emitter isn't registering the events.
Perhaps it might be best to instead have your runRequest function take in an array of SQL statements, and create the Request Object inside the function like so:
function runRequest(requests) {
return new Promise((resolve, reject) => {
var connection = new Connection(config)
connection.on('error', (err) => {
reject(err)
})
connection.on('debug', (txt) => {
// console.log(txt)
})
function processNextItem(request) {
// Create request object here, that takes a callback for handling error's
const request = new Request(requestSql, (err) => {
if (err) {
return reject(err)
}
if (requests.length <= 0) {
console.log('Done, Closing connection')
connection.close()
resolve()
} else {
// still have requests to process
processNextItem(requests.shift())
}
});
connection.execSql(request);
}
connection.connect((err) => {
if (err) {
reject(err)
}
processNextItem(requests.shift())
})
})
}
This way, if a certain request throws an error, you'll be sure to catch them.
Also there's no 'error' event for the Request
Are you sure? It's in the documentation.
@RobertSmart You're right, but it's documented poorly. The only time a error event is emitted on Request instances is if an error is encountered when a Request is prepared via Connection.prepare. 😞 In all other cases, the error is passed to the callback that is given when the Request is constructed.
The issue I have with passing in the SQL directly is that I have parameters, So can't build the queries first without assigning the parameters to the request object.
How can I get an error if the query does not run? for instance if I have a null value or something and the sql query is rejected, how can I catch that, and pass that back into my logic to handle the error. would that get picked up by the connection.prepare method?