Skip to content

Commit

Permalink
types added to some rpc errors
Browse files Browse the repository at this point in the history
  • Loading branch information
volovyks committed Feb 9, 2021
1 parent ca81785 commit e567f4a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/providers/json-rpc-provider.js

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

1 change: 1 addition & 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.

17 changes: 16 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.

8 changes: 5 additions & 3 deletions src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Network } from '../utils/network';
import { ConnectionInfo, fetchJson } from '../utils/web';
import { TypedError, ErrorContext } from '../utils/errors';
import { baseEncode } from 'borsh';
import { parseRpcError } from '../utils/rpc_errors';
import { parseRpcError, getErrorTypeFromErrorMessage } from '../utils/rpc_errors';
import { SignedTransaction } from '../transaction';

export { TypedError, ErrorContext };
Expand Down Expand Up @@ -78,7 +78,9 @@ export class JsonRpcProvider extends Provider {
result = await this.sendJsonRpc('query', [path, data]);
}
if (result && result.error) {
throw new Error(`Querying ${args} failed: ${result.error}.\n${JSON.stringify(result, null, 2)}`);
throw new TypedError(
`Querying ${args} failed: ${result.error}.\n${JSON.stringify(result, null, 2)}`,
getErrorTypeFromErrorMessage(result.error));
}
return result;
}
Expand Down Expand Up @@ -174,7 +176,7 @@ export class JsonRpcProvider extends Provider {
if (response.error.data === 'Timeout' || errorMessage.includes('Timeout error')) {
throw new TypedError('send_tx_commit has timed out.', 'TimeoutError');
} else {
throw new TypedError(errorMessage);
throw new TypedError(errorMessage, getErrorTypeFromErrorMessage(response.error.data));
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/utils/rpc_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ function walkSubtype(errorObj, schema, result, typeName) {
}
}

export function getErrorTypeFromErrorMessage(errorMessage) {
switch (true) {
case /^account .*? does not exist while viewing$/.test(errorMessage):
return 'AccountDoesNotExist';
case /^Account .*? doesn't exist$/.test(errorMessage):
return 'AccountDoesNotExist';
case /^access key .*? does not exist while viewing$/.test(errorMessage):
return 'AccessKeyDoesNotExist';
case /wasm execution failed with error: FunctionCallError\(CompilationError\(CodeDoesNotExist/.test(errorMessage):
return 'CodeDoesNotExist';
default:
return 'UntypedError';
}
}

/**
* Helper function determining if the argument is an object
* @param n Value to check
Expand Down

0 comments on commit e567f4a

Please sign in to comment.