Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate tx info into ServerErrors #388

Merged
merged 8 commits into from
Nov 16, 2020
2 changes: 1 addition & 1 deletion lib/account.js

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

17 changes: 8 additions & 9 deletions lib/transaction.d.ts

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

10 changes: 9 additions & 1 deletion lib/transaction.js

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

4 changes: 4 additions & 0 deletions lib/utils/rpc_errors.d.ts

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

15 changes: 14 additions & 1 deletion lib/utils/rpc_errors.js

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

4 changes: 2 additions & 2 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Connection } from './connection';
import {base_decode, base_encode} from './utils/serialize';
import { PublicKey } from './utils/key_pair';
import { PositionalArgsError } from './utils/errors';
import { parseRpcError } from './utils/rpc_errors';
import { parseRpcError, parseResultError } from './utils/rpc_errors';
import { ServerError } from './generated/rpc_error_types';

import exponentialBackoff from './utils/exponential-backoff';
Expand Down Expand Up @@ -182,7 +182,7 @@ export class Account {
`Transaction ${result.transaction_outcome.id} failed. ${result.status.Failure.error_message}`,
result.status.Failure.error_type);
} else {
throw parseRpcError(result.status.Failure);
throw parseResultError(result);
}
}
// TODO: if Tx is Unknown or Started.
Expand Down
16 changes: 8 additions & 8 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ export function functionCallAccessKey(receiverId: string, methodNames: String[],

export class IAction extends Assignable {}

class CreateAccount extends IAction {}
class DeployContract extends IAction { code: Uint8Array; }
class FunctionCall extends IAction { methodName: string; args: Uint8Array; gas: BN; deposit: BN; }
class Transfer extends IAction { deposit: BN; }
class Stake extends IAction { stake: BN; publicKey: PublicKey; }
class AddKey extends IAction { publicKey: PublicKey; accessKey: AccessKey; }
class DeleteKey extends IAction { publicKey: PublicKey; }
class DeleteAccount extends IAction { beneficiaryId: string; }
export class CreateAccount extends IAction {}
export class DeployContract extends IAction { code: Uint8Array; }
export class FunctionCall extends IAction { methodName: string; args: Uint8Array; gas: BN; deposit: BN; }
export class Transfer extends IAction { deposit: BN; }
export class Stake extends IAction { stake: BN; publicKey: PublicKey; }
export class AddKey extends IAction { publicKey: PublicKey; accessKey: AccessKey; }
export class DeleteKey extends IAction { publicKey: PublicKey; }
export class DeleteAccount extends IAction { beneficiaryId: string; }

export function createAccount(): Action {
return new Action({createAccount: new CreateAccount({}) });
Expand Down
14 changes: 14 additions & 0 deletions src/utils/rpc_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { ServerError } from '../generated/rpc_error_types';

export * from '../generated/rpc_error_types';

class ServerTransactionError extends ServerError {
public transaction_outcome: any;
}

export function parseRpcError(errorObj: Record<string, any>): ServerError {
const result = {};
const errorClassName = walkSubtype(errorObj, schema.schema, result, '');
Expand All @@ -16,6 +20,14 @@ export function parseRpcError(errorObj: Record<string, any>): ServerError {
return error;
}

export function parseResultError(result: any): ServerTransactionError {
const server_error = parseRpcError(result.status.Failure);
const server_tx_error = new ServerTransactionError();
Object.assign(server_tx_error, server_error);
server_tx_error.transaction_outcome = result.transaction_outcome;
return server_tx_error;
}

export function formatError(errorClassName: string, errorData): string {
if (typeof messages[errorClassName] === 'string') {
return Mustache.render(messages[errorClassName], errorData);
Expand Down Expand Up @@ -57,6 +69,8 @@ function walkSubtype(errorObj, schema, result, typeName) {
}
return walkSubtype(error, schema, result, errorTypeName);
} else {
// TODO: is this the right thing to do?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately I don't know, as I have no understanding of what are possible responses from nearcore side.

also I think we should completely remove everything related to error schema #331

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bowenwang1996 I feel like you may know a bit about the nearcore errors. Would you mind giving us your two cents when you get a moment this week?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an error only has kind if it is an action error but there are many other errors out there

result.kind = errorObj;
return typeName;
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/rpc_errors.test.js

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