Skip to content

Commit

Permalink
Add new staging support for error handling for nearlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny Kuzyakov committed Oct 4, 2019
1 parent 2485e64 commit cfe7af0
Show file tree
Hide file tree
Showing 89 changed files with 649 additions and 566 deletions.
62 changes: 41 additions & 21 deletions dist/nearlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ window.Buffer = Buffer;
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const transaction_1 = require("./transaction");
const provider_1 = require("./providers/provider");
const serialize_1 = require("./utils/serialize");
const key_pair_1 = require("./utils/key_pair");
// Default amount of tokens to be send with the function calls. Used to pay for the fees
Expand Down Expand Up @@ -62,8 +61,8 @@ class Account {
let waitTime = TX_STATUS_RETRY_WAIT;
for (let i = 0; i < TX_STATUS_RETRY_NUMBER; i++) {
result = await this.connection.provider.txStatus(txHash);
if (result.status === provider_1.FinalExecutionStatusBasic.Failure ||
typeof result.status === 'object' && typeof result.status.SuccessValue === 'string') {
if (typeof result.status === 'object' &&
(typeof result.status.SuccessValue === 'string' || typeof result.status.Failure === 'object')) {
return result;
}
await sleep(waitTime);
Expand Down Expand Up @@ -93,11 +92,8 @@ class Account {
}
const flatLogs = [result.transaction, ...result.receipts].reduce((acc, it) => acc.concat(it.outcome.logs), []);
this.printLogs(signedTx.transaction.receiverId, flatLogs);
if (result.status === provider_1.FinalExecutionStatusBasic.Failure) {
if (flatLogs) {
const errorMessage = flatLogs.find(it => it.startsWith('ABORT:')) || flatLogs.find(it => it.startsWith('Runtime error:')) || '';
throw new Error(`Transaction ${result.transaction.id} failed. ${errorMessage}`);
}
if (typeof result.status === 'object' && typeof result.status.Failure === 'object') {
throw new Error(`Transaction ${result.transaction.id} failed with ${result.status.Failure.error_type}. ${result.status.Failure.error_message}`);
}
// TODO: if Tx is Unknown or Started.
// TODO: deal with timeout on node side.
Expand Down Expand Up @@ -178,7 +174,7 @@ class Account {
exports.Account = Account;

}).call(this,require("buffer").Buffer)
},{"./providers/provider":16,"./transaction":18,"./utils/key_pair":21,"./utils/serialize":23,"buffer":32}],3:[function(require,module,exports){
},{"./transaction":18,"./utils/key_pair":21,"./utils/serialize":23,"buffer":32}],3:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
Expand Down Expand Up @@ -776,25 +772,18 @@ exports.JsonRpcProvider = JsonRpcProvider;
(function (Buffer){
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const enums_1 = require("../utils/enums");
var ExecutionStatusBasic;
(function (ExecutionStatusBasic) {
ExecutionStatusBasic["Unknown"] = "Unknown";
ExecutionStatusBasic["Pending"] = "Pending";
ExecutionStatusBasic["Failure"] = "Failure";
})(ExecutionStatusBasic = exports.ExecutionStatusBasic || (exports.ExecutionStatusBasic = {}));
class ExecutionStatus extends enums_1.Enum {
}
exports.ExecutionStatus = ExecutionStatus;
var FinalExecutionStatusBasic;
(function (FinalExecutionStatusBasic) {
FinalExecutionStatusBasic["NotStarted"] = "NotStarted";
FinalExecutionStatusBasic["Started"] = "Started";
FinalExecutionStatusBasic["Failure"] = "Failure";
})(FinalExecutionStatusBasic = exports.FinalExecutionStatusBasic || (exports.FinalExecutionStatusBasic = {}));
class FinalExecutionStatus extends enums_1.Enum {
}
exports.FinalExecutionStatus = FinalExecutionStatus;
var LegacyFinalTransactionStatus;
(function (LegacyFinalTransactionStatus) {
LegacyFinalTransactionStatus["Unknown"] = "Unknown";
Expand Down Expand Up @@ -831,7 +820,26 @@ function mapLegacyTransactionLog(tl) {
},
};
}
function fixLegacyBasicExecutionOutcomeFailure(t) {
if (t.outcome.status === ExecutionStatusBasic.Failure) {
t.outcome.status = {
Failure: {
error_message: t.outcome.logs.find(it => it.startsWith('ABORT:')) ||
t.outcome.logs.find(it => it.startsWith('Runtime error:')) || '',
error_type: 'LegacyError',
}
};
}
// Currently FunctionCallError doesn't return logged message in the error.
if (typeof t.outcome.status === 'object' && typeof t.outcome.status.Failure === 'object' &&
t.outcome.status.Failure.error_type === 'ActionError::FunctionCallError') {
t.outcome.status.Failure.error_message = t.outcome.logs.find(it => it.startsWith('ABORT:')) ||
t.outcome.status.Failure.error_message;
}
return t;
}
function adaptTransactionResult(txResult) {
// Fixing legacy transaction result
if ('transactions' in txResult) {
txResult = txResult;
let status;
Expand All @@ -857,15 +865,27 @@ function adaptTransactionResult(txResult) {
SuccessValue: result,
};
}
return {
status: status,
txResult = {
status,
transaction: mapLegacyTransactionLog(txResult.transactions.splice(0, 1)[0]),
receipts: txResult.transactions.map(mapLegacyTransactionLog),
};
}
else {
return txResult;
// Adapting from old error handling.
txResult.transaction = fixLegacyBasicExecutionOutcomeFailure(txResult.transaction);
txResult.receipts = txResult.receipts.map(fixLegacyBasicExecutionOutcomeFailure);
// Fixing master error status
if (txResult.status === FinalExecutionStatusBasic.Failure ||
(typeof txResult.status === 'object' && typeof txResult.status.Failure === 'object' &&
txResult.status.Failure.error_type === 'ActionError::FunctionCallError')) {
const err = [txResult.transaction, ...txResult.receipts]
.find(t => typeof t.outcome.status === 'object' && typeof t.outcome.status.Failure === 'object')
.outcome.status.Failure;
txResult.status = {
Failure: err
};
}
return txResult;
}
exports.adaptTransactionResult = adaptTransactionResult;
class Provider {
Expand All @@ -886,7 +906,7 @@ function getTransactionLastResult(txResult) {
exports.getTransactionLastResult = getTransactionLastResult;

}).call(this,require("buffer").Buffer)
},{"../utils/enums":19,"buffer":32}],17:[function(require,module,exports){
},{"buffer":32}],17:[function(require,module,exports){
'use strict';
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
Expand Down
10 changes: 5 additions & 5 deletions dist/nearlib.min.js

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions docs/nearlib/classes/_account_.account.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/nearlib/classes/_account_creator_.accountcreator.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions docs/nearlib/classes/_account_creator_.localaccountcreator.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cfe7af0

Please sign in to comment.