Adjust error logs on create (#3480)

This commit is contained in:
Jaco
2021-05-04 10:08:44 +02:00
committed by GitHub
parent 1db133b741
commit 11a3483ce5
6 changed files with 12 additions and 18 deletions
+4 -6
View File
@@ -52,7 +52,7 @@ function getSubType (value: TypeDef): keyof InterfaceTypes {
function getTypeClassMap (value: TypeDef): Record<string, keyof InterfaceTypes> {
const result: Record<string, keyof InterfaceTypes> = {};
return getSubDefArray(value).reduce((result, sub): Record<string, keyof InterfaceTypes> => {
return getSubDefArray(value).reduce<Record<string, keyof InterfaceTypes>>((result, sub) => {
result[sub.name as string] = sub.type as keyof InterfaceTypes;
return result;
@@ -61,9 +61,7 @@ function getTypeClassMap (value: TypeDef): Record<string, keyof InterfaceTypes>
// create an array of type string constructors from the input
function getTypeClassArray (value: TypeDef): (keyof InterfaceTypes)[] {
return getSubDefArray(value).map(({ type }): keyof InterfaceTypes =>
type as keyof InterfaceTypes
);
return getSubDefArray(value).map(({ type }) => type as keyof InterfaceTypes);
}
function createInt ({ displayName, length }: TypeDef, Clazz: typeof Int | typeof UInt): Constructor {
@@ -92,7 +90,7 @@ const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => C
return Enum.with(
subs.every(({ type }) => type === 'Null')
? subs.reduce((out: Record<string, number>, { index, name }, count): Record<string, number> => {
? subs.reduce<Record<string, number>>((out, { index, name }, count) => {
out[name as string] = index || count;
return out;
@@ -139,7 +137,7 @@ const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => C
const result: Record<string, number> = {};
return CodecSet.with(
getSubDefArray(value).reduce((result, { index, name }): Record<string, number> => {
getSubDefArray(value).reduce<Record<string, number>>((result, { index, name }) => {
result[name as string] = index as number;
return result;
+4 -9
View File
@@ -4,27 +4,24 @@
import type { Codec, Constructor, InterfaceTypes, Registry } from '../types';
import type { FromReg } from './types';
import { assert, isHex, isU8a, logger, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
import { assert, isHex, isU8a, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
import { createClass } from './createClass';
interface CreateOptions {
blockHash?: Uint8Array | string | null;
isPedantic?: boolean;
withoutLog?: boolean;
}
const l = logger('registry');
// With isPedantic, actually check that the encoding matches that supplied. This
// is much slower, but verifies that we have the correct types defined
// eslint-disable-next-line @typescript-eslint/ban-types
function checkInstance<T extends Codec = Codec, K extends string = string> (value: Uint8Array, created: FromReg<T, K>): void {
const u8a = created.toU8a();
const rawType = created.toRawType();
const isEqual = u8aEq(value, u8a) ||
// :code entries and singular bytes don't have length-prefixes (when from storage)
(rawType === 'Bytes' && value.length === u8a.length);
const isEqual = rawType === 'Bytes'
? value.length === u8a.length
: u8aEq(value, u8a);
assert(isEqual, () => `${rawType}:: Decoded input doesn't match input, received ${u8aToHex(value, 512)} (${value.length} bytes), created ${u8aToHex(u8a, 512)} (${u8a.length} bytes)`);
}
@@ -59,8 +56,6 @@ export function createTypeUnsafe<T extends Codec = Codec, K extends string = str
// it provides false warning which is more hinderance than help
return initType(registry, createClass<T, K>(registry, type), params, options);
} catch (error) {
!options.withoutLog && l.error(error);
throw new Error(`createType(${type}):: ${(error as Error).message}`);
}
}
+1 -1
View File
@@ -46,7 +46,7 @@ function encodeSubTypes (sub: TypeDef[], asEnum?: boolean): string {
assert(names.every((n) => !!n), () => `Subtypes does not have consistent names, ${names.join(', ')}`);
const inner = sub.reduce((result: Record<string, string>, type): Record<string, string> => ({
const inner = sub.reduce< Record<string, string>>((result, type) => ({
...result,
[type.name as string]: encodeTypeDef(type)
}), {});
+1 -1
View File
@@ -209,7 +209,7 @@ export function removeWrap (check: string): Mapper {
// eslint-disable-next-line @typescript-eslint/ban-types
export function sanitize (value: String | string, options?: SanitizeOptions): string {
return mappings.reduce((result, fn): string =>
return mappings.reduce<string>((result, fn) =>
fn(result, options), value.toString()
).trim();
}