Use util/assert (#30)

This commit is contained in:
Jaco Greeff
2017-12-03 09:46:15 +01:00
committed by GitHub
parent a4c92e744b
commit 51ea2bf988
4 changed files with 16 additions and 30 deletions
+3 -4
View File
@@ -3,7 +3,8 @@
import type { FormatterFunction } from './types';
const { isUndefined } = require('@polkadot/util/is');
const assert = require('@polkadot/util/assert');
const isUndefined = require('@polkadot/util/is/undefined');
const formatNoop = require('./noop');
const arrayTypeRegex = /\[\]$/;
@@ -25,9 +26,7 @@ function formatSingleType (formatters: { [any]: FormatterFunction }, type: strin
}
function formatArrayType (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
if (!Array.isArray(value)) {
throw new Error(`Unable to format non-array '${value}' as '${type}'`);
}
assert(Array.isArray(value), `Unable to format non-array '${value}' as '${type}'`);
type = type.replace(arrayTypeRegex, '');
+2 -3
View File
@@ -5,6 +5,7 @@ import type { ProviderInterface } from '../types';
require('./polyfill');
const assert = require('@polkadot/util/assert');
const JsonRpcCoder = require('../jsonRpcCoder');
module.exports = class HttpProvider extends JsonRpcCoder implements ProviderInterface {
@@ -28,9 +29,7 @@ module.exports = class HttpProvider extends JsonRpcCoder implements ProviderInte
method: 'POST'
});
if (!response.ok) {
throw new Error(`[${response.status}]: ${response.statusText}`);
}
assert(response.ok, `[${response.status}]: ${response.statusText}`);
const result = await response.json();
+7 -14
View File
@@ -3,33 +3,26 @@
import type { JsonRpcRequest, JsonRpcResponse } from './types';
const { isNumber, isUndefined } = require('@polkadot/util/is');
const assert = require('@polkadot/util/assert');
const isNumber = require('@polkadot/util/is/number');
const isUndefined = require('@polkadot/util/is/undefined');
module.exports = class JsonRpcCoder {
_id: number = 0;
decodeResponse (response: JsonRpcResponse): any {
if (!response) {
throw new Error('Empty response object received');
}
assert(response, 'Empty response object received');
if (response.jsonrpc !== '2.0') {
throw new Error('Invalid jsonrpc field in decoded object');
}
assert(response.jsonrpc === '2.0', 'Invalid jsonrpc field in decoded object');
if (!isNumber(response.id)) {
throw new Error('Invalid id field in decoded object');
}
assert(isNumber(response.id), 'Invalid id field in decoded object');
if (response.error) {
const { code, message } = response.error;
throw new Error(`[${code}]: ${message}`);
}
if (isUndefined(response.result)) {
throw new Error('No result found in JsonRpc response');
}
assert(!isUndefined(response.result), 'No result found in JsonRpc response');
return response.result;
}
+4 -9
View File
@@ -7,6 +7,7 @@ import type { ApiInterface } from './types';
const { formatInputs, formatOutput } = require('@polkadot/api-format');
const interfaces = require('@polkadot/api-jsonrpc');
const assert = require('@polkadot/util/assert');
const isFunction = require('@polkadot/util/is/function');
const jsonrpcSignature = require('@polkadot/util/jsonrpc/signature');
@@ -16,13 +17,9 @@ module.exports = class Api implements ApiInterface {
_stateInterface: any = null;
constructor (provider: ProviderInterface) {
if (!provider) {
throw new Error('Instantiate the Api with `new Api(new Provider(...))`');
}
assert(provider, 'Instantiate the Api with `new Api(new Provider(...))`');
if (!isFunction(provider.send)) {
throw new Error('Supplied provider does not expose send method');
}
assert(isFunction(provider.send), 'Supplied provider does not expose send method');
this._provider = provider;
@@ -49,9 +46,7 @@ module.exports = class Api implements ApiInterface {
container[method] = async (..._params: Array<any>): any => {
try {
if (inputs.length !== _params.length) {
throw new Error(`${inputs.length} params expected, found ${_params.length} instead`);
}
assert(inputs.length === _params.length, `${inputs.length} params expected, found ${_params.length} instead`);
const params = formatInputs(inputs, _params);
const result = await this._provider.send(rpcName, params);