Unhandled promise rejection warning in Ethereum - ethereum

I am very much struggling to find the solution to Error Snippet my problem. I am receiving errors.
for (address in accountAddress) {
try {
let code = await web3.eth.getCode(address)
//myCode.....
} catch (err) {
console.log(err)
}
}

Related

try/catch block can't stop ER_DUP_ENTRY error from crashing my appliction

Im adding a duplicate to a mysql table and I want to handle elicited ER_DUP_ENTRY error comming back with a Try/Catch block but its just crashing anyway , is there any possible way to handle error and stop application from crashing using a try/catch block?
async function init() {
try {
connection.query(
'SOME INSERT QUERY',
(err, result, feilds) => {
if (err) throw err
console.log(result);
}
);
} catch (e) {
console.log(e);
}
}
init();
The node mysql-library does not support promises out of the box, which means query does not return a promise which you can await. So you can either wrap the query function in a promise yourself:
async function init() {
try {
const duplicateResult = await new Promise((resolve, reject) => {
connection.query(
'SOME INSERT QUERY',
(err, result, fields) => {
if (err) {
return reject(err);
}
resolve(result);
});
});
} catch (e) {
console.log(e);
}
}
or use util.promisify as Always Learning posted alternatively.
The problem is that connection.query returns undefined right away. Your catch is not involved because the call ends before the work is done and will call your callback function later. An exception that occurs during your callback is too late. You try/catch block has already completed.
You can use promisify to wait on it like this though:
const util = require("util");
function init() {
const queryPromise = util.promisify(connection.query);
return queryPromise('SOME INSERT QUERY')
.catch(e => {
console.log("It failed", e);
});
}
init().then(result => {
if (result) console.log("It worked", result);
else console.log("Aww, it didn't work");
});

puppeteer - How I can close browser after page throw some error?

How do I close the browser with puppeteer when the page throws an error?
async function fnGoogle(page) {
try {
//some code here
await page.goto('blablalba');
} catch (e) {
throw e
}
}
Use browser.close() to close the browser, but you'd have to do it before re-throwing the error in your catch-block:
} catch (e) {
await browser.close()
throw e
}
demo

Do i need to close pool connection for every api in nodejs+mysql

My question is here i am creating pool for every API to get data from MySQL database, after query finish i am closing pool connection. Is there any performance issues if i do like this. what is best way to implement this.
Please excuse if i ask anything wrong, Thanks in advance.
firstapi: async (req, resp) => {
let connection;
try {
connection = await mysql.createPool(db);
let firstquery = "first query goes here";
const [firstapidata] = await connection.execute(verlaufevortagquery);
resp.json(firstapidata);
} catch (error) {
resp.status(500).json({ message: "Failed to execute query", Error: error });
}
},
secondapi: async (req, resp) => {
let connection;
try {
connection = await mysql.createPool(db);
let secondquery = "second query goes here";
const [secondata] = await connection.execute(verlaufevortagquery);
resp.json(secondata);
} catch (error) {
resp.status(500).json({ message: "Failed to execute query", Error: error });
}
}
Instead you can keep the connection open, there is no reason to open it every time
let connection;
connection = await mysql.createPool(db);
firstapi: async (req, resp) => {
try {
use connection.execute here
} catch (error) {
resp.status(500).json({ message: "Failed to execute query", Error: error });
}
},
secondapi: async (req, resp) => {
try {
use connection.execute here
} catch (error) {
resp.status(500).json({ message: "Failed to execute query", Error: error });
}
}

await - catch error - UnhandledPromiseRejectionWarning

I'm getting
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 44): Error: fail
main.js
import { request } from './api'
async getData({ commit, state }, ids ){
try {
var x = await request(ids)
commit('getData', x.data)
} catch ( e ) {
console.log('request failed get',e.code,e.errno)
}
}
api.js
export async function request(type,url,ids){
axios.get('localhost/data')
.then(function (response) {
return Promise.resolve(response.data)
})
.catch(function (e) {
return Promise.reject(new Error('fail'))
})
}
How can I handle the promise rejection? Shouldn't the try catch block catch the error from the await function here?
You are mixing up async/await with promises. In api.js, there is no need to use the async keyword. The async keyword makes it so that anything you return from the function is wrapped in a promise, which you don't need, since axios.get returns a promise already.
Also, you forgot to actually return the promise from Axios, your request function just returns undefined.
Lastly, you don't have to return promises from the then and catch methods, just return a value, or throw an error.
If you rewrite the function like this it should work as expected:
export function request(type,url,ids){
return axios.get('localhost/data')
.then(function (response) {
return response.data
})
.catch(function (e) {
throw new Error('fail')
})
}

Recover from Uncaught Exception in Node.JS

OK, so I have a problem. If an uncaught exception occurs while I am handling an HTTP request, I have no opportunity to call the end() method on the http.ServerResponse object. Therefore, the server hangs forever and never fulfills the request.
Here's an example:
var express = require('express');
var app = express.createServer();
var reqNum = 0;
app.get('/favicon.ico', function(req, res) {res.send(404);});
app.get('*', function(req, res, next) {
console.log("Request #", ++reqNum, ":", req.url);
next();
});
app.get('/error', function(req, res, next) {
throw new Error("Problem occurred");
});
app.get('/hang', function(req, res, next) {
console.log("In /hang route");
setTimeout(function() {
console.log("In /hang callback");
if(reqNum >= 3)
throw new Error("Problem occurred");
res.send("It worked!");
}, 2000);
});
process.on('uncaughtException', function(err) {
console.log("Uncaught exception!", err);
});
app.listen(8080);
If you visit /error, an exception occurs, but it is caught. The user receives an error message - no problem. If I visit /hang, though, the server will eventually throw an uncaught exception and hang forever. Any subsequent requests for /hang will hang.
This sucks. Any advice for how to fix this issue?
When an uncaught exception occurs, you're in an unclean state. Let the process die and restart it, there's nothing else you can do to safely bring it back to a known-good state. Use forever, it'll restart your process as soon as it dies.
If error is thrown synchronously, express won't stop working, only returning 500.
this.app.get("/error", (request, response) => {
throw new Error("shouldn't stop");
});
If error is thrown asynchronously, express will crash. But according to it's official documentation, there is still a way to recover from it by calling next:
this.app.get("/error", (request, response, next) => {
setTimeout(() => {
try {
throw new Error("shouldn't stop");
} catch (err) {
next(err);
}
}, 0);
});
This will let express do its duty to response with a 500 error.
Use try/catch/finally.
app.get('/hang', function(req, res, next) {
console.log("In /hang route");
setTimeout(function() {
console.log("In /hang callback");
try {
if(reqNum >= 3)
throw new Error("Problem occurred");
} catch (err) {
console.log("There was an error", err);
} finally {
res.send("It worked!");
}
}, 2000);
});