Error on Javascript file, treated on Main


#1

I have a Connection.js file, Where get the data from the database.
but, if i have a query error, like sintax ,for example.
how can i throw the error to main process ?

main method who calls Connection with Try Catch

ipcMain.on('get-data-quadro1', (event, arg) => {
  try {
let objPicking = tools.getQuadro1();
let recordsetPicking;
objPicking.then(function (result) {
  recordsetPicking = result;
  event.sender.send('mostra-na-tela-quadro1', recordsetPicking);
})
  } catch (error) {
event.sender.send('errorInWindow')
  }

})

#2

Well, so long as your connection thingy really is executed anywhere within mains try-block, you can throw errors with throw:

// connection.js
// [...]
if (err) {
  throw 'Syntax error: ' + err; // This is assuming the variable err can be converted to string
} else {
// [...]

And within your catch:

// main
try {
// [...]
} catch (error) {
  console.log(error); // Output: 'Syntax error: xyz'
  event.sender.send('errorInWindow');
}

Do proper error handling and logging instead of console.log though.

That’s about the basics anyway. Unless I completely misunderstood the question?


#3

Throw the error on screen ,but doesnt enter in catch on Main. :frowning: