This is typically solved by supplying a callback function when you send the transaction:
web3.eth.sendTransaction(txParams, function (error, txhash) {
if (error) {
// error handling
}
else {
// handling of successful transaction
}
or
web3.eth.sendRawTransaction(serializedTx, function (error, txhash) {
if (error) {
// error handling
}
else {
// handling of successful transaction
}
The callback function is invoked either when the transaction was processed successfully or after the transaction ran into an error. If there was an error, the error parameter of the callback function contains an error description, if not the error parameter is undefined. Callback functions have an error first callback style and are generally possible, see the web3.js documentation.
For transactions that are already mined, you can check the value of getTransactionReceipt(txhash).status, which is 0 if the transaction failed an 1 if the transaction was successful. For more information see questions Transaction Status and How do I detect a failed transaction after the Byzantium fork as the REVERT opcode does not consume all gas?. Note that this works only for transactions after the Byzantinum Fork, which was at block 4370000. For transactions made earlier, you can check if the gas spent is equal to max gas, if so you can assume that there was a transaction failure.
1.0.0-beta.24– phoebus Nov 22 '17 at 16:28