1.sendSignedTransactiondeでのerror処理
・errorオブジェクトから、errorの値を取り出してresponceで返す
2.
・下のようなコードでのPromiseで受け取ったerrorオブジェクトから、errorの値を取り出す
const [ hash, err ] =
await eth.sendSignedTransaction(web3, '0x'+serializedTx.toString('hex'));
3.errorの値を取り出してresponceで返すstep
(1) error objectをstringに変換
(2) 不要なwordを削除
(3) responceのendに渡す
const eth = require("./eth.js");
・・・
const [ hash, _err ] = await eth.sendSignedTransaction(web3, '0x'+serializedTx.toString('hex'));
if (_err) {
let ERR_KEYWORD = "Error: Returned error: ";
let err = _err.toString();
if(err.indexOf( ERR_KEYWORD ) !== -1) {
err = err.replace( ERR_KEYWORD,"");
}
return res.status(400).end(err)
}
4../eth.js
module.exports = {
sendSignedTransaction : _sendSignedTransaction,
}
async function _sendSignedTransaction(web3, transactionObject) {
let hash;
try {
hash = await __sendSignedTransaction(web3, transactionObject) ;
} catch (err) {
return [false, err];
}
return [hash, null];
function __sendSignedTransaction(web3, transactionObject) {
return new Promise((resolve, reject) => {
web3.eth.sendSignedTransaction(transactionObject, (error, hash) => {
if( error ) {
return reject(error);
}
resolve (hash);
});
});
}
}